inode.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/time.h>
  5. #include <linux/fs.h>
  6. #include "reiserfs.h"
  7. #include "acl.h"
  8. #include "xattr.h"
  9. #include <linux/exportfs.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/highmem.h>
  12. #include <linux/slab.h>
  13. #include <linux/uaccess.h>
  14. #include <asm/unaligned.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/mpage.h>
  17. #include <linux/writeback.h>
  18. #include <linux/quotaops.h>
  19. #include <linux/swap.h>
  20. #include <linux/uio.h>
  21. #include <linux/bio.h>
  22. int reiserfs_commit_write(struct file *f, struct page *page,
  23. unsigned from, unsigned to);
  24. void reiserfs_evict_inode(struct inode *inode)
  25. {
  26. /*
  27. * We need blocks for transaction + (user+group) quota
  28. * update (possibly delete)
  29. */
  30. int jbegin_count =
  31. JOURNAL_PER_BALANCE_CNT * 2 +
  32. 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb);
  33. struct reiserfs_transaction_handle th;
  34. int err;
  35. if (!inode->i_nlink && !is_bad_inode(inode))
  36. dquot_initialize(inode);
  37. truncate_inode_pages_final(&inode->i_data);
  38. if (inode->i_nlink)
  39. goto no_delete;
  40. /*
  41. * The = 0 happens when we abort creating a new inode
  42. * for some reason like lack of space..
  43. * also handles bad_inode case
  44. */
  45. if (!(inode->i_state & I_NEW) && INODE_PKEY(inode)->k_objectid != 0) {
  46. reiserfs_delete_xattrs(inode);
  47. reiserfs_write_lock(inode->i_sb);
  48. if (journal_begin(&th, inode->i_sb, jbegin_count))
  49. goto out;
  50. reiserfs_update_inode_transaction(inode);
  51. reiserfs_discard_prealloc(&th, inode);
  52. err = reiserfs_delete_object(&th, inode);
  53. /*
  54. * Do quota update inside a transaction for journaled quotas.
  55. * We must do that after delete_object so that quota updates
  56. * go into the same transaction as stat data deletion
  57. */
  58. if (!err) {
  59. int depth = reiserfs_write_unlock_nested(inode->i_sb);
  60. dquot_free_inode(inode);
  61. reiserfs_write_lock_nested(inode->i_sb, depth);
  62. }
  63. if (journal_end(&th))
  64. goto out;
  65. /*
  66. * check return value from reiserfs_delete_object after
  67. * ending the transaction
  68. */
  69. if (err)
  70. goto out;
  71. /*
  72. * all items of file are deleted, so we can remove
  73. * "save" link
  74. * we can't do anything about an error here
  75. */
  76. remove_save_link(inode, 0 /* not truncate */);
  77. out:
  78. reiserfs_write_unlock(inode->i_sb);
  79. } else {
  80. /* no object items are in the tree */
  81. ;
  82. }
  83. /* note this must go after the journal_end to prevent deadlock */
  84. clear_inode(inode);
  85. dquot_drop(inode);
  86. inode->i_blocks = 0;
  87. return;
  88. no_delete:
  89. clear_inode(inode);
  90. dquot_drop(inode);
  91. }
  92. static void _make_cpu_key(struct cpu_key *key, int version, __u32 dirid,
  93. __u32 objectid, loff_t offset, int type, int length)
  94. {
  95. key->version = version;
  96. key->on_disk_key.k_dir_id = dirid;
  97. key->on_disk_key.k_objectid = objectid;
  98. set_cpu_key_k_offset(key, offset);
  99. set_cpu_key_k_type(key, type);
  100. key->key_length = length;
  101. }
  102. /*
  103. * take base of inode_key (it comes from inode always) (dirid, objectid)
  104. * and version from an inode, set offset and type of key
  105. */
  106. void make_cpu_key(struct cpu_key *key, struct inode *inode, loff_t offset,
  107. int type, int length)
  108. {
  109. _make_cpu_key(key, get_inode_item_key_version(inode),
  110. le32_to_cpu(INODE_PKEY(inode)->k_dir_id),
  111. le32_to_cpu(INODE_PKEY(inode)->k_objectid), offset, type,
  112. length);
  113. }
  114. /* when key is 0, do not set version and short key */
  115. inline void make_le_item_head(struct item_head *ih, const struct cpu_key *key,
  116. int version,
  117. loff_t offset, int type, int length,
  118. int entry_count /*or ih_free_space */ )
  119. {
  120. if (key) {
  121. ih->ih_key.k_dir_id = cpu_to_le32(key->on_disk_key.k_dir_id);
  122. ih->ih_key.k_objectid =
  123. cpu_to_le32(key->on_disk_key.k_objectid);
  124. }
  125. put_ih_version(ih, version);
  126. set_le_ih_k_offset(ih, offset);
  127. set_le_ih_k_type(ih, type);
  128. put_ih_item_len(ih, length);
  129. /* set_ih_free_space (ih, 0); */
  130. /*
  131. * for directory items it is entry count, for directs and stat
  132. * datas - 0xffff, for indirects - 0
  133. */
  134. put_ih_entry_count(ih, entry_count);
  135. }
  136. /*
  137. * FIXME: we might cache recently accessed indirect item
  138. * Ugh. Not too eager for that....
  139. * I cut the code until such time as I see a convincing argument (benchmark).
  140. * I don't want a bloated inode struct..., and I don't like code complexity....
  141. */
  142. /*
  143. * cutting the code is fine, since it really isn't in use yet and is easy
  144. * to add back in. But, Vladimir has a really good idea here. Think
  145. * about what happens for reading a file. For each page,
  146. * The VFS layer calls reiserfs_read_folio, who searches the tree to find
  147. * an indirect item. This indirect item has X number of pointers, where
  148. * X is a big number if we've done the block allocation right. But,
  149. * we only use one or two of these pointers during each call to read_folio,
  150. * needlessly researching again later on.
  151. *
  152. * The size of the cache could be dynamic based on the size of the file.
  153. *
  154. * I'd also like to see us cache the location the stat data item, since
  155. * we are needlessly researching for that frequently.
  156. *
  157. * --chris
  158. */
  159. /*
  160. * If this page has a file tail in it, and
  161. * it was read in by get_block_create_0, the page data is valid,
  162. * but tail is still sitting in a direct item, and we can't write to
  163. * it. So, look through this page, and check all the mapped buffers
  164. * to make sure they have valid block numbers. Any that don't need
  165. * to be unmapped, so that __block_write_begin will correctly call
  166. * reiserfs_get_block to convert the tail into an unformatted node
  167. */
  168. static inline void fix_tail_page_for_writing(struct page *page)
  169. {
  170. struct buffer_head *head, *next, *bh;
  171. if (page && page_has_buffers(page)) {
  172. head = page_buffers(page);
  173. bh = head;
  174. do {
  175. next = bh->b_this_page;
  176. if (buffer_mapped(bh) && bh->b_blocknr == 0) {
  177. reiserfs_unmap_buffer(bh);
  178. }
  179. bh = next;
  180. } while (bh != head);
  181. }
  182. }
  183. /*
  184. * reiserfs_get_block does not need to allocate a block only if it has been
  185. * done already or non-hole position has been found in the indirect item
  186. */
  187. static inline int allocation_needed(int retval, b_blocknr_t allocated,
  188. struct item_head *ih,
  189. __le32 * item, int pos_in_item)
  190. {
  191. if (allocated)
  192. return 0;
  193. if (retval == POSITION_FOUND && is_indirect_le_ih(ih) &&
  194. get_block_num(item, pos_in_item))
  195. return 0;
  196. return 1;
  197. }
  198. static inline int indirect_item_found(int retval, struct item_head *ih)
  199. {
  200. return (retval == POSITION_FOUND) && is_indirect_le_ih(ih);
  201. }
  202. static inline void set_block_dev_mapped(struct buffer_head *bh,
  203. b_blocknr_t block, struct inode *inode)
  204. {
  205. map_bh(bh, inode->i_sb, block);
  206. }
  207. /*
  208. * files which were created in the earlier version can not be longer,
  209. * than 2 gb
  210. */
  211. static int file_capable(struct inode *inode, sector_t block)
  212. {
  213. /* it is new file. */
  214. if (get_inode_item_key_version(inode) != KEY_FORMAT_3_5 ||
  215. /* old file, but 'block' is inside of 2gb */
  216. block < (1 << (31 - inode->i_sb->s_blocksize_bits)))
  217. return 1;
  218. return 0;
  219. }
  220. static int restart_transaction(struct reiserfs_transaction_handle *th,
  221. struct inode *inode, struct treepath *path)
  222. {
  223. struct super_block *s = th->t_super;
  224. int err;
  225. BUG_ON(!th->t_trans_id);
  226. BUG_ON(!th->t_refcount);
  227. pathrelse(path);
  228. /* we cannot restart while nested */
  229. if (th->t_refcount > 1) {
  230. return 0;
  231. }
  232. reiserfs_update_sd(th, inode);
  233. err = journal_end(th);
  234. if (!err) {
  235. err = journal_begin(th, s, JOURNAL_PER_BALANCE_CNT * 6);
  236. if (!err)
  237. reiserfs_update_inode_transaction(inode);
  238. }
  239. return err;
  240. }
  241. /*
  242. * it is called by get_block when create == 0. Returns block number
  243. * for 'block'-th logical block of file. When it hits direct item it
  244. * returns 0 (being called from bmap) or read direct item into piece
  245. * of page (bh_result)
  246. * Please improve the english/clarity in the comment above, as it is
  247. * hard to understand.
  248. */
  249. static int _get_block_create_0(struct inode *inode, sector_t block,
  250. struct buffer_head *bh_result, int args)
  251. {
  252. INITIALIZE_PATH(path);
  253. struct cpu_key key;
  254. struct buffer_head *bh;
  255. struct item_head *ih, tmp_ih;
  256. b_blocknr_t blocknr;
  257. char *p;
  258. int chars;
  259. int ret;
  260. int result;
  261. int done = 0;
  262. unsigned long offset;
  263. /* prepare the key to look for the 'block'-th block of file */
  264. make_cpu_key(&key, inode,
  265. (loff_t) block * inode->i_sb->s_blocksize + 1, TYPE_ANY,
  266. 3);
  267. result = search_for_position_by_key(inode->i_sb, &key, &path);
  268. if (result != POSITION_FOUND) {
  269. pathrelse(&path);
  270. if (result == IO_ERROR)
  271. return -EIO;
  272. /*
  273. * We do not return -ENOENT if there is a hole but page is
  274. * uptodate, because it means that there is some MMAPED data
  275. * associated with it that is yet to be written to disk.
  276. */
  277. if ((args & GET_BLOCK_NO_HOLE)
  278. && !PageUptodate(bh_result->b_page)) {
  279. return -ENOENT;
  280. }
  281. return 0;
  282. }
  283. bh = get_last_bh(&path);
  284. ih = tp_item_head(&path);
  285. if (is_indirect_le_ih(ih)) {
  286. __le32 *ind_item = (__le32 *) ih_item_body(bh, ih);
  287. /*
  288. * FIXME: here we could cache indirect item or part of it in
  289. * the inode to avoid search_by_key in case of subsequent
  290. * access to file
  291. */
  292. blocknr = get_block_num(ind_item, path.pos_in_item);
  293. ret = 0;
  294. if (blocknr) {
  295. map_bh(bh_result, inode->i_sb, blocknr);
  296. if (path.pos_in_item ==
  297. ((ih_item_len(ih) / UNFM_P_SIZE) - 1)) {
  298. set_buffer_boundary(bh_result);
  299. }
  300. } else
  301. /*
  302. * We do not return -ENOENT if there is a hole but
  303. * page is uptodate, because it means that there is
  304. * some MMAPED data associated with it that is
  305. * yet to be written to disk.
  306. */
  307. if ((args & GET_BLOCK_NO_HOLE)
  308. && !PageUptodate(bh_result->b_page)) {
  309. ret = -ENOENT;
  310. }
  311. pathrelse(&path);
  312. return ret;
  313. }
  314. /* requested data are in direct item(s) */
  315. if (!(args & GET_BLOCK_READ_DIRECT)) {
  316. /*
  317. * we are called by bmap. FIXME: we can not map block of file
  318. * when it is stored in direct item(s)
  319. */
  320. pathrelse(&path);
  321. return -ENOENT;
  322. }
  323. /*
  324. * if we've got a direct item, and the buffer or page was uptodate,
  325. * we don't want to pull data off disk again. skip to the
  326. * end, where we map the buffer and return
  327. */
  328. if (buffer_uptodate(bh_result)) {
  329. goto finished;
  330. } else
  331. /*
  332. * grab_tail_page can trigger calls to reiserfs_get_block on
  333. * up to date pages without any buffers. If the page is up
  334. * to date, we don't want read old data off disk. Set the up
  335. * to date bit on the buffer instead and jump to the end
  336. */
  337. if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
  338. set_buffer_uptodate(bh_result);
  339. goto finished;
  340. }
  341. /* read file tail into part of page */
  342. offset = (cpu_key_k_offset(&key) - 1) & (PAGE_SIZE - 1);
  343. copy_item_head(&tmp_ih, ih);
  344. /*
  345. * we only want to kmap if we are reading the tail into the page.
  346. * this is not the common case, so we don't kmap until we are
  347. * sure we need to. But, this means the item might move if
  348. * kmap schedules
  349. */
  350. p = (char *)kmap(bh_result->b_page);
  351. p += offset;
  352. memset(p, 0, inode->i_sb->s_blocksize);
  353. do {
  354. if (!is_direct_le_ih(ih)) {
  355. BUG();
  356. }
  357. /*
  358. * make sure we don't read more bytes than actually exist in
  359. * the file. This can happen in odd cases where i_size isn't
  360. * correct, and when direct item padding results in a few
  361. * extra bytes at the end of the direct item
  362. */
  363. if ((le_ih_k_offset(ih) + path.pos_in_item) > inode->i_size)
  364. break;
  365. if ((le_ih_k_offset(ih) - 1 + ih_item_len(ih)) > inode->i_size) {
  366. chars =
  367. inode->i_size - (le_ih_k_offset(ih) - 1) -
  368. path.pos_in_item;
  369. done = 1;
  370. } else {
  371. chars = ih_item_len(ih) - path.pos_in_item;
  372. }
  373. memcpy(p, ih_item_body(bh, ih) + path.pos_in_item, chars);
  374. if (done)
  375. break;
  376. p += chars;
  377. /*
  378. * we done, if read direct item is not the last item of
  379. * node FIXME: we could try to check right delimiting key
  380. * to see whether direct item continues in the right
  381. * neighbor or rely on i_size
  382. */
  383. if (PATH_LAST_POSITION(&path) != (B_NR_ITEMS(bh) - 1))
  384. break;
  385. /* update key to look for the next piece */
  386. set_cpu_key_k_offset(&key, cpu_key_k_offset(&key) + chars);
  387. result = search_for_position_by_key(inode->i_sb, &key, &path);
  388. if (result != POSITION_FOUND)
  389. /* i/o error most likely */
  390. break;
  391. bh = get_last_bh(&path);
  392. ih = tp_item_head(&path);
  393. } while (1);
  394. flush_dcache_page(bh_result->b_page);
  395. kunmap(bh_result->b_page);
  396. finished:
  397. pathrelse(&path);
  398. if (result == IO_ERROR)
  399. return -EIO;
  400. /*
  401. * this buffer has valid data, but isn't valid for io. mapping it to
  402. * block #0 tells the rest of reiserfs it just has a tail in it
  403. */
  404. map_bh(bh_result, inode->i_sb, 0);
  405. set_buffer_uptodate(bh_result);
  406. return 0;
  407. }
  408. /*
  409. * this is called to create file map. So, _get_block_create_0 will not
  410. * read direct item
  411. */
  412. static int reiserfs_bmap(struct inode *inode, sector_t block,
  413. struct buffer_head *bh_result, int create)
  414. {
  415. if (!file_capable(inode, block))
  416. return -EFBIG;
  417. reiserfs_write_lock(inode->i_sb);
  418. /* do not read the direct item */
  419. _get_block_create_0(inode, block, bh_result, 0);
  420. reiserfs_write_unlock(inode->i_sb);
  421. return 0;
  422. }
  423. /*
  424. * special version of get_block that is only used by grab_tail_page right
  425. * now. It is sent to __block_write_begin, and when you try to get a
  426. * block past the end of the file (or a block from a hole) it returns
  427. * -ENOENT instead of a valid buffer. __block_write_begin expects to
  428. * be able to do i/o on the buffers returned, unless an error value
  429. * is also returned.
  430. *
  431. * So, this allows __block_write_begin to be used for reading a single block
  432. * in a page. Where it does not produce a valid page for holes, or past the
  433. * end of the file. This turns out to be exactly what we need for reading
  434. * tails for conversion.
  435. *
  436. * The point of the wrapper is forcing a certain value for create, even
  437. * though the VFS layer is calling this function with create==1. If you
  438. * don't want to send create == GET_BLOCK_NO_HOLE to reiserfs_get_block,
  439. * don't use this function.
  440. */
  441. static int reiserfs_get_block_create_0(struct inode *inode, sector_t block,
  442. struct buffer_head *bh_result,
  443. int create)
  444. {
  445. return reiserfs_get_block(inode, block, bh_result, GET_BLOCK_NO_HOLE);
  446. }
  447. /*
  448. * This is special helper for reiserfs_get_block in case we are executing
  449. * direct_IO request.
  450. */
  451. static int reiserfs_get_blocks_direct_io(struct inode *inode,
  452. sector_t iblock,
  453. struct buffer_head *bh_result,
  454. int create)
  455. {
  456. int ret;
  457. bh_result->b_page = NULL;
  458. /*
  459. * We set the b_size before reiserfs_get_block call since it is
  460. * referenced in convert_tail_for_hole() that may be called from
  461. * reiserfs_get_block()
  462. */
  463. bh_result->b_size = i_blocksize(inode);
  464. ret = reiserfs_get_block(inode, iblock, bh_result,
  465. create | GET_BLOCK_NO_DANGLE);
  466. if (ret)
  467. goto out;
  468. /* don't allow direct io onto tail pages */
  469. if (buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
  470. /*
  471. * make sure future calls to the direct io funcs for this
  472. * offset in the file fail by unmapping the buffer
  473. */
  474. clear_buffer_mapped(bh_result);
  475. ret = -EINVAL;
  476. }
  477. /*
  478. * Possible unpacked tail. Flush the data before pages have
  479. * disappeared
  480. */
  481. if (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) {
  482. int err;
  483. reiserfs_write_lock(inode->i_sb);
  484. err = reiserfs_commit_for_inode(inode);
  485. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  486. reiserfs_write_unlock(inode->i_sb);
  487. if (err < 0)
  488. ret = err;
  489. }
  490. out:
  491. return ret;
  492. }
  493. /*
  494. * helper function for when reiserfs_get_block is called for a hole
  495. * but the file tail is still in a direct item
  496. * bh_result is the buffer head for the hole
  497. * tail_offset is the offset of the start of the tail in the file
  498. *
  499. * This calls prepare_write, which will start a new transaction
  500. * you should not be in a transaction, or have any paths held when you
  501. * call this.
  502. */
  503. static int convert_tail_for_hole(struct inode *inode,
  504. struct buffer_head *bh_result,
  505. loff_t tail_offset)
  506. {
  507. unsigned long index;
  508. unsigned long tail_end;
  509. unsigned long tail_start;
  510. struct page *tail_page;
  511. struct page *hole_page = bh_result->b_page;
  512. int retval = 0;
  513. if ((tail_offset & (bh_result->b_size - 1)) != 1)
  514. return -EIO;
  515. /* always try to read until the end of the block */
  516. tail_start = tail_offset & (PAGE_SIZE - 1);
  517. tail_end = (tail_start | (bh_result->b_size - 1)) + 1;
  518. index = tail_offset >> PAGE_SHIFT;
  519. /*
  520. * hole_page can be zero in case of direct_io, we are sure
  521. * that we cannot get here if we write with O_DIRECT into tail page
  522. */
  523. if (!hole_page || index != hole_page->index) {
  524. tail_page = grab_cache_page(inode->i_mapping, index);
  525. retval = -ENOMEM;
  526. if (!tail_page) {
  527. goto out;
  528. }
  529. } else {
  530. tail_page = hole_page;
  531. }
  532. /*
  533. * we don't have to make sure the conversion did not happen while
  534. * we were locking the page because anyone that could convert
  535. * must first take i_mutex.
  536. *
  537. * We must fix the tail page for writing because it might have buffers
  538. * that are mapped, but have a block number of 0. This indicates tail
  539. * data that has been read directly into the page, and
  540. * __block_write_begin won't trigger a get_block in this case.
  541. */
  542. fix_tail_page_for_writing(tail_page);
  543. retval = __reiserfs_write_begin(tail_page, tail_start,
  544. tail_end - tail_start);
  545. if (retval)
  546. goto unlock;
  547. /* tail conversion might change the data in the page */
  548. flush_dcache_page(tail_page);
  549. retval = reiserfs_commit_write(NULL, tail_page, tail_start, tail_end);
  550. unlock:
  551. if (tail_page != hole_page) {
  552. unlock_page(tail_page);
  553. put_page(tail_page);
  554. }
  555. out:
  556. return retval;
  557. }
  558. static inline int _allocate_block(struct reiserfs_transaction_handle *th,
  559. sector_t block,
  560. struct inode *inode,
  561. b_blocknr_t * allocated_block_nr,
  562. struct treepath *path, int flags)
  563. {
  564. BUG_ON(!th->t_trans_id);
  565. #ifdef REISERFS_PREALLOCATE
  566. if (!(flags & GET_BLOCK_NO_IMUX)) {
  567. return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
  568. path, block);
  569. }
  570. #endif
  571. return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
  572. block);
  573. }
  574. int reiserfs_get_block(struct inode *inode, sector_t block,
  575. struct buffer_head *bh_result, int create)
  576. {
  577. int repeat, retval = 0;
  578. /* b_blocknr_t is (unsigned) 32 bit int*/
  579. b_blocknr_t allocated_block_nr = 0;
  580. INITIALIZE_PATH(path);
  581. int pos_in_item;
  582. struct cpu_key key;
  583. struct buffer_head *bh, *unbh = NULL;
  584. struct item_head *ih, tmp_ih;
  585. __le32 *item;
  586. int done;
  587. int fs_gen;
  588. struct reiserfs_transaction_handle *th = NULL;
  589. /*
  590. * space reserved in transaction batch:
  591. * . 3 balancings in direct->indirect conversion
  592. * . 1 block involved into reiserfs_update_sd()
  593. * XXX in practically impossible worst case direct2indirect()
  594. * can incur (much) more than 3 balancings.
  595. * quota update for user, group
  596. */
  597. int jbegin_count =
  598. JOURNAL_PER_BALANCE_CNT * 3 + 1 +
  599. 2 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  600. int version;
  601. int dangle = 1;
  602. loff_t new_offset =
  603. (((loff_t) block) << inode->i_sb->s_blocksize_bits) + 1;
  604. reiserfs_write_lock(inode->i_sb);
  605. version = get_inode_item_key_version(inode);
  606. if (!file_capable(inode, block)) {
  607. reiserfs_write_unlock(inode->i_sb);
  608. return -EFBIG;
  609. }
  610. /*
  611. * if !create, we aren't changing the FS, so we don't need to
  612. * log anything, so we don't need to start a transaction
  613. */
  614. if (!(create & GET_BLOCK_CREATE)) {
  615. int ret;
  616. /* find number of block-th logical block of the file */
  617. ret = _get_block_create_0(inode, block, bh_result,
  618. create | GET_BLOCK_READ_DIRECT);
  619. reiserfs_write_unlock(inode->i_sb);
  620. return ret;
  621. }
  622. /*
  623. * if we're already in a transaction, make sure to close
  624. * any new transactions we start in this func
  625. */
  626. if ((create & GET_BLOCK_NO_DANGLE) ||
  627. reiserfs_transaction_running(inode->i_sb))
  628. dangle = 0;
  629. /*
  630. * If file is of such a size, that it might have a tail and
  631. * tails are enabled we should mark it as possibly needing
  632. * tail packing on close
  633. */
  634. if ((have_large_tails(inode->i_sb)
  635. && inode->i_size < i_block_size(inode) * 4)
  636. || (have_small_tails(inode->i_sb)
  637. && inode->i_size < i_block_size(inode)))
  638. REISERFS_I(inode)->i_flags |= i_pack_on_close_mask;
  639. /* set the key of the first byte in the 'block'-th block of file */
  640. make_cpu_key(&key, inode, new_offset, TYPE_ANY, 3 /*key length */ );
  641. if ((new_offset + inode->i_sb->s_blocksize - 1) > inode->i_size) {
  642. start_trans:
  643. th = reiserfs_persistent_transaction(inode->i_sb, jbegin_count);
  644. if (!th) {
  645. retval = -ENOMEM;
  646. goto failure;
  647. }
  648. reiserfs_update_inode_transaction(inode);
  649. }
  650. research:
  651. retval = search_for_position_by_key(inode->i_sb, &key, &path);
  652. if (retval == IO_ERROR) {
  653. retval = -EIO;
  654. goto failure;
  655. }
  656. bh = get_last_bh(&path);
  657. ih = tp_item_head(&path);
  658. item = tp_item_body(&path);
  659. pos_in_item = path.pos_in_item;
  660. fs_gen = get_generation(inode->i_sb);
  661. copy_item_head(&tmp_ih, ih);
  662. if (allocation_needed
  663. (retval, allocated_block_nr, ih, item, pos_in_item)) {
  664. /* we have to allocate block for the unformatted node */
  665. if (!th) {
  666. pathrelse(&path);
  667. goto start_trans;
  668. }
  669. repeat =
  670. _allocate_block(th, block, inode, &allocated_block_nr,
  671. &path, create);
  672. /*
  673. * restart the transaction to give the journal a chance to free
  674. * some blocks. releases the path, so we have to go back to
  675. * research if we succeed on the second try
  676. */
  677. if (repeat == NO_DISK_SPACE || repeat == QUOTA_EXCEEDED) {
  678. SB_JOURNAL(inode->i_sb)->j_next_async_flush = 1;
  679. retval = restart_transaction(th, inode, &path);
  680. if (retval)
  681. goto failure;
  682. repeat =
  683. _allocate_block(th, block, inode,
  684. &allocated_block_nr, NULL, create);
  685. if (repeat != NO_DISK_SPACE && repeat != QUOTA_EXCEEDED) {
  686. goto research;
  687. }
  688. if (repeat == QUOTA_EXCEEDED)
  689. retval = -EDQUOT;
  690. else
  691. retval = -ENOSPC;
  692. goto failure;
  693. }
  694. if (fs_changed(fs_gen, inode->i_sb)
  695. && item_moved(&tmp_ih, &path)) {
  696. goto research;
  697. }
  698. }
  699. if (indirect_item_found(retval, ih)) {
  700. b_blocknr_t unfm_ptr;
  701. /*
  702. * 'block'-th block is in the file already (there is
  703. * corresponding cell in some indirect item). But it may be
  704. * zero unformatted node pointer (hole)
  705. */
  706. unfm_ptr = get_block_num(item, pos_in_item);
  707. if (unfm_ptr == 0) {
  708. /* use allocated block to plug the hole */
  709. reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
  710. if (fs_changed(fs_gen, inode->i_sb)
  711. && item_moved(&tmp_ih, &path)) {
  712. reiserfs_restore_prepared_buffer(inode->i_sb,
  713. bh);
  714. goto research;
  715. }
  716. set_buffer_new(bh_result);
  717. if (buffer_dirty(bh_result)
  718. && reiserfs_data_ordered(inode->i_sb))
  719. reiserfs_add_ordered_list(inode, bh_result);
  720. put_block_num(item, pos_in_item, allocated_block_nr);
  721. unfm_ptr = allocated_block_nr;
  722. journal_mark_dirty(th, bh);
  723. reiserfs_update_sd(th, inode);
  724. }
  725. set_block_dev_mapped(bh_result, unfm_ptr, inode);
  726. pathrelse(&path);
  727. retval = 0;
  728. if (!dangle && th)
  729. retval = reiserfs_end_persistent_transaction(th);
  730. reiserfs_write_unlock(inode->i_sb);
  731. /*
  732. * the item was found, so new blocks were not added to the file
  733. * there is no need to make sure the inode is updated with this
  734. * transaction
  735. */
  736. return retval;
  737. }
  738. if (!th) {
  739. pathrelse(&path);
  740. goto start_trans;
  741. }
  742. /*
  743. * desired position is not found or is in the direct item. We have
  744. * to append file with holes up to 'block'-th block converting
  745. * direct items to indirect one if necessary
  746. */
  747. done = 0;
  748. do {
  749. if (is_statdata_le_ih(ih)) {
  750. __le32 unp = 0;
  751. struct cpu_key tmp_key;
  752. /* indirect item has to be inserted */
  753. make_le_item_head(&tmp_ih, &key, version, 1,
  754. TYPE_INDIRECT, UNFM_P_SIZE,
  755. 0 /* free_space */ );
  756. /*
  757. * we are going to add 'block'-th block to the file.
  758. * Use allocated block for that
  759. */
  760. if (cpu_key_k_offset(&key) == 1) {
  761. unp = cpu_to_le32(allocated_block_nr);
  762. set_block_dev_mapped(bh_result,
  763. allocated_block_nr, inode);
  764. set_buffer_new(bh_result);
  765. done = 1;
  766. }
  767. tmp_key = key; /* ;) */
  768. set_cpu_key_k_offset(&tmp_key, 1);
  769. PATH_LAST_POSITION(&path)++;
  770. retval =
  771. reiserfs_insert_item(th, &path, &tmp_key, &tmp_ih,
  772. inode, (char *)&unp);
  773. if (retval) {
  774. reiserfs_free_block(th, inode,
  775. allocated_block_nr, 1);
  776. /*
  777. * retval == -ENOSPC, -EDQUOT or -EIO
  778. * or -EEXIST
  779. */
  780. goto failure;
  781. }
  782. } else if (is_direct_le_ih(ih)) {
  783. /* direct item has to be converted */
  784. loff_t tail_offset;
  785. tail_offset =
  786. ((le_ih_k_offset(ih) -
  787. 1) & ~(inode->i_sb->s_blocksize - 1)) + 1;
  788. /*
  789. * direct item we just found fits into block we have
  790. * to map. Convert it into unformatted node: use
  791. * bh_result for the conversion
  792. */
  793. if (tail_offset == cpu_key_k_offset(&key)) {
  794. set_block_dev_mapped(bh_result,
  795. allocated_block_nr, inode);
  796. unbh = bh_result;
  797. done = 1;
  798. } else {
  799. /*
  800. * we have to pad file tail stored in direct
  801. * item(s) up to block size and convert it
  802. * to unformatted node. FIXME: this should
  803. * also get into page cache
  804. */
  805. pathrelse(&path);
  806. /*
  807. * ugly, but we can only end the transaction if
  808. * we aren't nested
  809. */
  810. BUG_ON(!th->t_refcount);
  811. if (th->t_refcount == 1) {
  812. retval =
  813. reiserfs_end_persistent_transaction
  814. (th);
  815. th = NULL;
  816. if (retval)
  817. goto failure;
  818. }
  819. retval =
  820. convert_tail_for_hole(inode, bh_result,
  821. tail_offset);
  822. if (retval) {
  823. if (retval != -ENOSPC)
  824. reiserfs_error(inode->i_sb,
  825. "clm-6004",
  826. "convert tail failed "
  827. "inode %lu, error %d",
  828. inode->i_ino,
  829. retval);
  830. if (allocated_block_nr) {
  831. /*
  832. * the bitmap, the super,
  833. * and the stat data == 3
  834. */
  835. if (!th)
  836. th = reiserfs_persistent_transaction(inode->i_sb, 3);
  837. if (th)
  838. reiserfs_free_block(th,
  839. inode,
  840. allocated_block_nr,
  841. 1);
  842. }
  843. goto failure;
  844. }
  845. goto research;
  846. }
  847. retval =
  848. direct2indirect(th, inode, &path, unbh,
  849. tail_offset);
  850. if (retval) {
  851. reiserfs_unmap_buffer(unbh);
  852. reiserfs_free_block(th, inode,
  853. allocated_block_nr, 1);
  854. goto failure;
  855. }
  856. /*
  857. * it is important the set_buffer_uptodate is done
  858. * after the direct2indirect. The buffer might
  859. * contain valid data newer than the data on disk
  860. * (read by read_folio, changed, and then sent here by
  861. * writepage). direct2indirect needs to know if unbh
  862. * was already up to date, so it can decide if the
  863. * data in unbh needs to be replaced with data from
  864. * the disk
  865. */
  866. set_buffer_uptodate(unbh);
  867. /*
  868. * unbh->b_page == NULL in case of DIRECT_IO request,
  869. * this means buffer will disappear shortly, so it
  870. * should not be added to
  871. */
  872. if (unbh->b_page) {
  873. /*
  874. * we've converted the tail, so we must
  875. * flush unbh before the transaction commits
  876. */
  877. reiserfs_add_tail_list(inode, unbh);
  878. /*
  879. * mark it dirty now to prevent commit_write
  880. * from adding this buffer to the inode's
  881. * dirty buffer list
  882. */
  883. /*
  884. * AKPM: changed __mark_buffer_dirty to
  885. * mark_buffer_dirty(). It's still atomic,
  886. * but it sets the page dirty too, which makes
  887. * it eligible for writeback at any time by the
  888. * VM (which was also the case with
  889. * __mark_buffer_dirty())
  890. */
  891. mark_buffer_dirty(unbh);
  892. }
  893. } else {
  894. /*
  895. * append indirect item with holes if needed, when
  896. * appending pointer to 'block'-th block use block,
  897. * which is already allocated
  898. */
  899. struct cpu_key tmp_key;
  900. /*
  901. * We use this in case we need to allocate
  902. * only one block which is a fastpath
  903. */
  904. unp_t unf_single = 0;
  905. unp_t *un;
  906. __u64 max_to_insert =
  907. MAX_ITEM_LEN(inode->i_sb->s_blocksize) /
  908. UNFM_P_SIZE;
  909. __u64 blocks_needed;
  910. RFALSE(pos_in_item != ih_item_len(ih) / UNFM_P_SIZE,
  911. "vs-804: invalid position for append");
  912. /*
  913. * indirect item has to be appended,
  914. * set up key of that position
  915. * (key type is unimportant)
  916. */
  917. make_cpu_key(&tmp_key, inode,
  918. le_key_k_offset(version,
  919. &ih->ih_key) +
  920. op_bytes_number(ih,
  921. inode->i_sb->s_blocksize),
  922. TYPE_INDIRECT, 3);
  923. RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
  924. "green-805: invalid offset");
  925. blocks_needed =
  926. 1 +
  927. ((cpu_key_k_offset(&key) -
  928. cpu_key_k_offset(&tmp_key)) >> inode->i_sb->
  929. s_blocksize_bits);
  930. if (blocks_needed == 1) {
  931. un = &unf_single;
  932. } else {
  933. un = kcalloc(min(blocks_needed, max_to_insert),
  934. UNFM_P_SIZE, GFP_NOFS);
  935. if (!un) {
  936. un = &unf_single;
  937. blocks_needed = 1;
  938. max_to_insert = 0;
  939. }
  940. }
  941. if (blocks_needed <= max_to_insert) {
  942. /*
  943. * we are going to add target block to
  944. * the file. Use allocated block for that
  945. */
  946. un[blocks_needed - 1] =
  947. cpu_to_le32(allocated_block_nr);
  948. set_block_dev_mapped(bh_result,
  949. allocated_block_nr, inode);
  950. set_buffer_new(bh_result);
  951. done = 1;
  952. } else {
  953. /* paste hole to the indirect item */
  954. /*
  955. * If kcalloc failed, max_to_insert becomes
  956. * zero and it means we only have space for
  957. * one block
  958. */
  959. blocks_needed =
  960. max_to_insert ? max_to_insert : 1;
  961. }
  962. retval =
  963. reiserfs_paste_into_item(th, &path, &tmp_key, inode,
  964. (char *)un,
  965. UNFM_P_SIZE *
  966. blocks_needed);
  967. if (blocks_needed != 1)
  968. kfree(un);
  969. if (retval) {
  970. reiserfs_free_block(th, inode,
  971. allocated_block_nr, 1);
  972. goto failure;
  973. }
  974. if (!done) {
  975. /*
  976. * We need to mark new file size in case
  977. * this function will be interrupted/aborted
  978. * later on. And we may do this only for
  979. * holes.
  980. */
  981. inode->i_size +=
  982. inode->i_sb->s_blocksize * blocks_needed;
  983. }
  984. }
  985. if (done == 1)
  986. break;
  987. /*
  988. * this loop could log more blocks than we had originally
  989. * asked for. So, we have to allow the transaction to end
  990. * if it is too big or too full. Update the inode so things
  991. * are consistent if we crash before the function returns
  992. * release the path so that anybody waiting on the path before
  993. * ending their transaction will be able to continue.
  994. */
  995. if (journal_transaction_should_end(th, th->t_blocks_allocated)) {
  996. retval = restart_transaction(th, inode, &path);
  997. if (retval)
  998. goto failure;
  999. }
  1000. /*
  1001. * inserting indirect pointers for a hole can take a
  1002. * long time. reschedule if needed and also release the write
  1003. * lock for others.
  1004. */
  1005. reiserfs_cond_resched(inode->i_sb);
  1006. retval = search_for_position_by_key(inode->i_sb, &key, &path);
  1007. if (retval == IO_ERROR) {
  1008. retval = -EIO;
  1009. goto failure;
  1010. }
  1011. if (retval == POSITION_FOUND) {
  1012. reiserfs_warning(inode->i_sb, "vs-825",
  1013. "%K should not be found", &key);
  1014. retval = -EEXIST;
  1015. if (allocated_block_nr)
  1016. reiserfs_free_block(th, inode,
  1017. allocated_block_nr, 1);
  1018. pathrelse(&path);
  1019. goto failure;
  1020. }
  1021. bh = get_last_bh(&path);
  1022. ih = tp_item_head(&path);
  1023. item = tp_item_body(&path);
  1024. pos_in_item = path.pos_in_item;
  1025. } while (1);
  1026. retval = 0;
  1027. failure:
  1028. if (th && (!dangle || (retval && !th->t_trans_id))) {
  1029. int err;
  1030. if (th->t_trans_id)
  1031. reiserfs_update_sd(th, inode);
  1032. err = reiserfs_end_persistent_transaction(th);
  1033. if (err)
  1034. retval = err;
  1035. }
  1036. reiserfs_write_unlock(inode->i_sb);
  1037. reiserfs_check_path(&path);
  1038. return retval;
  1039. }
  1040. static void reiserfs_readahead(struct readahead_control *rac)
  1041. {
  1042. mpage_readahead(rac, reiserfs_get_block);
  1043. }
  1044. /*
  1045. * Compute real number of used bytes by file
  1046. * Following three functions can go away when we'll have enough space in
  1047. * stat item
  1048. */
  1049. static int real_space_diff(struct inode *inode, int sd_size)
  1050. {
  1051. int bytes;
  1052. loff_t blocksize = inode->i_sb->s_blocksize;
  1053. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
  1054. return sd_size;
  1055. /*
  1056. * End of file is also in full block with indirect reference, so round
  1057. * up to the next block.
  1058. *
  1059. * there is just no way to know if the tail is actually packed
  1060. * on the file, so we have to assume it isn't. When we pack the
  1061. * tail, we add 4 bytes to pretend there really is an unformatted
  1062. * node pointer
  1063. */
  1064. bytes =
  1065. ((inode->i_size +
  1066. (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
  1067. sd_size;
  1068. return bytes;
  1069. }
  1070. static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
  1071. int sd_size)
  1072. {
  1073. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
  1074. return inode->i_size +
  1075. (loff_t) (real_space_diff(inode, sd_size));
  1076. }
  1077. return ((loff_t) real_space_diff(inode, sd_size)) +
  1078. (((loff_t) blocks) << 9);
  1079. }
  1080. /* Compute number of blocks used by file in ReiserFS counting */
  1081. static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
  1082. {
  1083. loff_t bytes = inode_get_bytes(inode);
  1084. loff_t real_space = real_space_diff(inode, sd_size);
  1085. /* keeps fsck and non-quota versions of reiserfs happy */
  1086. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
  1087. bytes += (loff_t) 511;
  1088. }
  1089. /*
  1090. * files from before the quota patch might i_blocks such that
  1091. * bytes < real_space. Deal with that here to prevent it from
  1092. * going negative.
  1093. */
  1094. if (bytes < real_space)
  1095. return 0;
  1096. return (bytes - real_space) >> 9;
  1097. }
  1098. /*
  1099. * BAD: new directories have stat data of new type and all other items
  1100. * of old type. Version stored in the inode says about body items, so
  1101. * in update_stat_data we can not rely on inode, but have to check
  1102. * item version directly
  1103. */
  1104. /* called by read_locked_inode */
  1105. static void init_inode(struct inode *inode, struct treepath *path)
  1106. {
  1107. struct buffer_head *bh;
  1108. struct item_head *ih;
  1109. __u32 rdev;
  1110. bh = PATH_PLAST_BUFFER(path);
  1111. ih = tp_item_head(path);
  1112. copy_key(INODE_PKEY(inode), &ih->ih_key);
  1113. INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
  1114. REISERFS_I(inode)->i_flags = 0;
  1115. REISERFS_I(inode)->i_prealloc_block = 0;
  1116. REISERFS_I(inode)->i_prealloc_count = 0;
  1117. REISERFS_I(inode)->i_trans_id = 0;
  1118. REISERFS_I(inode)->i_jl = NULL;
  1119. reiserfs_init_xattr_rwsem(inode);
  1120. if (stat_data_v1(ih)) {
  1121. struct stat_data_v1 *sd =
  1122. (struct stat_data_v1 *)ih_item_body(bh, ih);
  1123. unsigned long blocks;
  1124. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1125. set_inode_sd_version(inode, STAT_DATA_V1);
  1126. inode->i_mode = sd_v1_mode(sd);
  1127. set_nlink(inode, sd_v1_nlink(sd));
  1128. i_uid_write(inode, sd_v1_uid(sd));
  1129. i_gid_write(inode, sd_v1_gid(sd));
  1130. inode->i_size = sd_v1_size(sd);
  1131. inode->i_atime.tv_sec = sd_v1_atime(sd);
  1132. inode->i_mtime.tv_sec = sd_v1_mtime(sd);
  1133. inode->i_ctime.tv_sec = sd_v1_ctime(sd);
  1134. inode->i_atime.tv_nsec = 0;
  1135. inode->i_ctime.tv_nsec = 0;
  1136. inode->i_mtime.tv_nsec = 0;
  1137. inode->i_blocks = sd_v1_blocks(sd);
  1138. inode->i_generation = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1139. blocks = (inode->i_size + 511) >> 9;
  1140. blocks = _ROUND_UP(blocks, inode->i_sb->s_blocksize >> 9);
  1141. /*
  1142. * there was a bug in <=3.5.23 when i_blocks could take
  1143. * negative values. Starting from 3.5.17 this value could
  1144. * even be stored in stat data. For such files we set
  1145. * i_blocks based on file size. Just 2 notes: this can be
  1146. * wrong for sparse files. On-disk value will be only
  1147. * updated if file's inode will ever change
  1148. */
  1149. if (inode->i_blocks > blocks) {
  1150. inode->i_blocks = blocks;
  1151. }
  1152. rdev = sd_v1_rdev(sd);
  1153. REISERFS_I(inode)->i_first_direct_byte =
  1154. sd_v1_first_direct_byte(sd);
  1155. /*
  1156. * an early bug in the quota code can give us an odd
  1157. * number for the block count. This is incorrect, fix it here.
  1158. */
  1159. if (inode->i_blocks & 1) {
  1160. inode->i_blocks++;
  1161. }
  1162. inode_set_bytes(inode,
  1163. to_real_used_space(inode, inode->i_blocks,
  1164. SD_V1_SIZE));
  1165. /*
  1166. * nopack is initially zero for v1 objects. For v2 objects,
  1167. * nopack is initialised from sd_attrs
  1168. */
  1169. REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
  1170. } else {
  1171. /*
  1172. * new stat data found, but object may have old items
  1173. * (directories and symlinks)
  1174. */
  1175. struct stat_data *sd = (struct stat_data *)ih_item_body(bh, ih);
  1176. inode->i_mode = sd_v2_mode(sd);
  1177. set_nlink(inode, sd_v2_nlink(sd));
  1178. i_uid_write(inode, sd_v2_uid(sd));
  1179. inode->i_size = sd_v2_size(sd);
  1180. i_gid_write(inode, sd_v2_gid(sd));
  1181. inode->i_mtime.tv_sec = sd_v2_mtime(sd);
  1182. inode->i_atime.tv_sec = sd_v2_atime(sd);
  1183. inode->i_ctime.tv_sec = sd_v2_ctime(sd);
  1184. inode->i_ctime.tv_nsec = 0;
  1185. inode->i_mtime.tv_nsec = 0;
  1186. inode->i_atime.tv_nsec = 0;
  1187. inode->i_blocks = sd_v2_blocks(sd);
  1188. rdev = sd_v2_rdev(sd);
  1189. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1190. inode->i_generation =
  1191. le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1192. else
  1193. inode->i_generation = sd_v2_generation(sd);
  1194. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1195. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1196. else
  1197. set_inode_item_key_version(inode, KEY_FORMAT_3_6);
  1198. REISERFS_I(inode)->i_first_direct_byte = 0;
  1199. set_inode_sd_version(inode, STAT_DATA_V2);
  1200. inode_set_bytes(inode,
  1201. to_real_used_space(inode, inode->i_blocks,
  1202. SD_V2_SIZE));
  1203. /*
  1204. * read persistent inode attributes from sd and initialise
  1205. * generic inode flags from them
  1206. */
  1207. REISERFS_I(inode)->i_attrs = sd_v2_attrs(sd);
  1208. sd_attrs_to_i_attrs(sd_v2_attrs(sd), inode);
  1209. }
  1210. pathrelse(path);
  1211. if (S_ISREG(inode->i_mode)) {
  1212. inode->i_op = &reiserfs_file_inode_operations;
  1213. inode->i_fop = &reiserfs_file_operations;
  1214. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  1215. } else if (S_ISDIR(inode->i_mode)) {
  1216. inode->i_op = &reiserfs_dir_inode_operations;
  1217. inode->i_fop = &reiserfs_dir_operations;
  1218. } else if (S_ISLNK(inode->i_mode)) {
  1219. inode->i_op = &reiserfs_symlink_inode_operations;
  1220. inode_nohighmem(inode);
  1221. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  1222. } else {
  1223. inode->i_blocks = 0;
  1224. inode->i_op = &reiserfs_special_inode_operations;
  1225. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  1226. }
  1227. }
  1228. /* update new stat data with inode fields */
  1229. static void inode2sd(void *sd, struct inode *inode, loff_t size)
  1230. {
  1231. struct stat_data *sd_v2 = (struct stat_data *)sd;
  1232. set_sd_v2_mode(sd_v2, inode->i_mode);
  1233. set_sd_v2_nlink(sd_v2, inode->i_nlink);
  1234. set_sd_v2_uid(sd_v2, i_uid_read(inode));
  1235. set_sd_v2_size(sd_v2, size);
  1236. set_sd_v2_gid(sd_v2, i_gid_read(inode));
  1237. set_sd_v2_mtime(sd_v2, inode->i_mtime.tv_sec);
  1238. set_sd_v2_atime(sd_v2, inode->i_atime.tv_sec);
  1239. set_sd_v2_ctime(sd_v2, inode->i_ctime.tv_sec);
  1240. set_sd_v2_blocks(sd_v2, to_fake_used_blocks(inode, SD_V2_SIZE));
  1241. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1242. set_sd_v2_rdev(sd_v2, new_encode_dev(inode->i_rdev));
  1243. else
  1244. set_sd_v2_generation(sd_v2, inode->i_generation);
  1245. set_sd_v2_attrs(sd_v2, REISERFS_I(inode)->i_attrs);
  1246. }
  1247. /* used to copy inode's fields to old stat data */
  1248. static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
  1249. {
  1250. struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
  1251. set_sd_v1_mode(sd_v1, inode->i_mode);
  1252. set_sd_v1_uid(sd_v1, i_uid_read(inode));
  1253. set_sd_v1_gid(sd_v1, i_gid_read(inode));
  1254. set_sd_v1_nlink(sd_v1, inode->i_nlink);
  1255. set_sd_v1_size(sd_v1, size);
  1256. set_sd_v1_atime(sd_v1, inode->i_atime.tv_sec);
  1257. set_sd_v1_ctime(sd_v1, inode->i_ctime.tv_sec);
  1258. set_sd_v1_mtime(sd_v1, inode->i_mtime.tv_sec);
  1259. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1260. set_sd_v1_rdev(sd_v1, new_encode_dev(inode->i_rdev));
  1261. else
  1262. set_sd_v1_blocks(sd_v1, to_fake_used_blocks(inode, SD_V1_SIZE));
  1263. /* Sigh. i_first_direct_byte is back */
  1264. set_sd_v1_first_direct_byte(sd_v1,
  1265. REISERFS_I(inode)->i_first_direct_byte);
  1266. }
  1267. /*
  1268. * NOTE, you must prepare the buffer head before sending it here,
  1269. * and then log it after the call
  1270. */
  1271. static void update_stat_data(struct treepath *path, struct inode *inode,
  1272. loff_t size)
  1273. {
  1274. struct buffer_head *bh;
  1275. struct item_head *ih;
  1276. bh = PATH_PLAST_BUFFER(path);
  1277. ih = tp_item_head(path);
  1278. if (!is_statdata_le_ih(ih))
  1279. reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
  1280. INODE_PKEY(inode), ih);
  1281. /* path points to old stat data */
  1282. if (stat_data_v1(ih)) {
  1283. inode2sd_v1(ih_item_body(bh, ih), inode, size);
  1284. } else {
  1285. inode2sd(ih_item_body(bh, ih), inode, size);
  1286. }
  1287. return;
  1288. }
  1289. void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
  1290. struct inode *inode, loff_t size)
  1291. {
  1292. struct cpu_key key;
  1293. INITIALIZE_PATH(path);
  1294. struct buffer_head *bh;
  1295. int fs_gen;
  1296. struct item_head *ih, tmp_ih;
  1297. int retval;
  1298. BUG_ON(!th->t_trans_id);
  1299. /* key type is unimportant */
  1300. make_cpu_key(&key, inode, SD_OFFSET, TYPE_STAT_DATA, 3);
  1301. for (;;) {
  1302. int pos;
  1303. /* look for the object's stat data */
  1304. retval = search_item(inode->i_sb, &key, &path);
  1305. if (retval == IO_ERROR) {
  1306. reiserfs_error(inode->i_sb, "vs-13050",
  1307. "i/o failure occurred trying to "
  1308. "update %K stat data", &key);
  1309. return;
  1310. }
  1311. if (retval == ITEM_NOT_FOUND) {
  1312. pos = PATH_LAST_POSITION(&path);
  1313. pathrelse(&path);
  1314. if (inode->i_nlink == 0) {
  1315. /*reiserfs_warning (inode->i_sb, "vs-13050: reiserfs_update_sd: i_nlink == 0, stat data not found"); */
  1316. return;
  1317. }
  1318. reiserfs_warning(inode->i_sb, "vs-13060",
  1319. "stat data of object %k (nlink == %d) "
  1320. "not found (pos %d)",
  1321. INODE_PKEY(inode), inode->i_nlink,
  1322. pos);
  1323. reiserfs_check_path(&path);
  1324. return;
  1325. }
  1326. /*
  1327. * sigh, prepare_for_journal might schedule. When it
  1328. * schedules the FS might change. We have to detect that,
  1329. * and loop back to the search if the stat data item has moved
  1330. */
  1331. bh = get_last_bh(&path);
  1332. ih = tp_item_head(&path);
  1333. copy_item_head(&tmp_ih, ih);
  1334. fs_gen = get_generation(inode->i_sb);
  1335. reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
  1336. /* Stat_data item has been moved after scheduling. */
  1337. if (fs_changed(fs_gen, inode->i_sb)
  1338. && item_moved(&tmp_ih, &path)) {
  1339. reiserfs_restore_prepared_buffer(inode->i_sb, bh);
  1340. continue;
  1341. }
  1342. break;
  1343. }
  1344. update_stat_data(&path, inode, size);
  1345. journal_mark_dirty(th, bh);
  1346. pathrelse(&path);
  1347. return;
  1348. }
  1349. /*
  1350. * reiserfs_read_locked_inode is called to read the inode off disk, and it
  1351. * does a make_bad_inode when things go wrong. But, we need to make sure
  1352. * and clear the key in the private portion of the inode, otherwise a
  1353. * corresponding iput might try to delete whatever object the inode last
  1354. * represented.
  1355. */
  1356. static void reiserfs_make_bad_inode(struct inode *inode)
  1357. {
  1358. memset(INODE_PKEY(inode), 0, KEY_SIZE);
  1359. make_bad_inode(inode);
  1360. }
  1361. /*
  1362. * initially this function was derived from minix or ext2's analog and
  1363. * evolved as the prototype did
  1364. */
  1365. int reiserfs_init_locked_inode(struct inode *inode, void *p)
  1366. {
  1367. struct reiserfs_iget_args *args = (struct reiserfs_iget_args *)p;
  1368. inode->i_ino = args->objectid;
  1369. INODE_PKEY(inode)->k_dir_id = cpu_to_le32(args->dirid);
  1370. return 0;
  1371. }
  1372. /*
  1373. * looks for stat data in the tree, and fills up the fields of in-core
  1374. * inode stat data fields
  1375. */
  1376. void reiserfs_read_locked_inode(struct inode *inode,
  1377. struct reiserfs_iget_args *args)
  1378. {
  1379. INITIALIZE_PATH(path_to_sd);
  1380. struct cpu_key key;
  1381. unsigned long dirino;
  1382. int retval;
  1383. dirino = args->dirid;
  1384. /*
  1385. * set version 1, version 2 could be used too, because stat data
  1386. * key is the same in both versions
  1387. */
  1388. _make_cpu_key(&key, KEY_FORMAT_3_5, dirino, inode->i_ino, 0, 0, 3);
  1389. /* look for the object's stat data */
  1390. retval = search_item(inode->i_sb, &key, &path_to_sd);
  1391. if (retval == IO_ERROR) {
  1392. reiserfs_error(inode->i_sb, "vs-13070",
  1393. "i/o failure occurred trying to find "
  1394. "stat data of %K", &key);
  1395. reiserfs_make_bad_inode(inode);
  1396. return;
  1397. }
  1398. /* a stale NFS handle can trigger this without it being an error */
  1399. if (retval != ITEM_FOUND) {
  1400. pathrelse(&path_to_sd);
  1401. reiserfs_make_bad_inode(inode);
  1402. clear_nlink(inode);
  1403. return;
  1404. }
  1405. init_inode(inode, &path_to_sd);
  1406. /*
  1407. * It is possible that knfsd is trying to access inode of a file
  1408. * that is being removed from the disk by some other thread. As we
  1409. * update sd on unlink all that is required is to check for nlink
  1410. * here. This bug was first found by Sizif when debugging
  1411. * SquidNG/Butterfly, forgotten, and found again after Philippe
  1412. * Gramoulle <[email protected]> reproduced it.
  1413. * More logical fix would require changes in fs/inode.c:iput() to
  1414. * remove inode from hash-table _after_ fs cleaned disk stuff up and
  1415. * in iget() to return NULL if I_FREEING inode is found in
  1416. * hash-table.
  1417. */
  1418. /*
  1419. * Currently there is one place where it's ok to meet inode with
  1420. * nlink==0: processing of open-unlinked and half-truncated files
  1421. * during mount (fs/reiserfs/super.c:finish_unfinished()).
  1422. */
  1423. if ((inode->i_nlink == 0) &&
  1424. !REISERFS_SB(inode->i_sb)->s_is_unlinked_ok) {
  1425. reiserfs_warning(inode->i_sb, "vs-13075",
  1426. "dead inode read from disk %K. "
  1427. "This is likely to be race with knfsd. Ignore",
  1428. &key);
  1429. reiserfs_make_bad_inode(inode);
  1430. }
  1431. /* init inode should be relsing */
  1432. reiserfs_check_path(&path_to_sd);
  1433. /*
  1434. * Stat data v1 doesn't support ACLs.
  1435. */
  1436. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1437. cache_no_acl(inode);
  1438. }
  1439. /*
  1440. * reiserfs_find_actor() - "find actor" reiserfs supplies to iget5_locked().
  1441. *
  1442. * @inode: inode from hash table to check
  1443. * @opaque: "cookie" passed to iget5_locked(). This is &reiserfs_iget_args.
  1444. *
  1445. * This function is called by iget5_locked() to distinguish reiserfs inodes
  1446. * having the same inode numbers. Such inodes can only exist due to some
  1447. * error condition. One of them should be bad. Inodes with identical
  1448. * inode numbers (objectids) are distinguished by parent directory ids.
  1449. *
  1450. */
  1451. int reiserfs_find_actor(struct inode *inode, void *opaque)
  1452. {
  1453. struct reiserfs_iget_args *args;
  1454. args = opaque;
  1455. /* args is already in CPU order */
  1456. return (inode->i_ino == args->objectid) &&
  1457. (le32_to_cpu(INODE_PKEY(inode)->k_dir_id) == args->dirid);
  1458. }
  1459. struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
  1460. {
  1461. struct inode *inode;
  1462. struct reiserfs_iget_args args;
  1463. int depth;
  1464. args.objectid = key->on_disk_key.k_objectid;
  1465. args.dirid = key->on_disk_key.k_dir_id;
  1466. depth = reiserfs_write_unlock_nested(s);
  1467. inode = iget5_locked(s, key->on_disk_key.k_objectid,
  1468. reiserfs_find_actor, reiserfs_init_locked_inode,
  1469. (void *)(&args));
  1470. reiserfs_write_lock_nested(s, depth);
  1471. if (!inode)
  1472. return ERR_PTR(-ENOMEM);
  1473. if (inode->i_state & I_NEW) {
  1474. reiserfs_read_locked_inode(inode, &args);
  1475. unlock_new_inode(inode);
  1476. }
  1477. if (comp_short_keys(INODE_PKEY(inode), key) || is_bad_inode(inode)) {
  1478. /* either due to i/o error or a stale NFS handle */
  1479. iput(inode);
  1480. inode = NULL;
  1481. }
  1482. return inode;
  1483. }
  1484. static struct dentry *reiserfs_get_dentry(struct super_block *sb,
  1485. u32 objectid, u32 dir_id, u32 generation)
  1486. {
  1487. struct cpu_key key;
  1488. struct inode *inode;
  1489. key.on_disk_key.k_objectid = objectid;
  1490. key.on_disk_key.k_dir_id = dir_id;
  1491. reiserfs_write_lock(sb);
  1492. inode = reiserfs_iget(sb, &key);
  1493. if (inode && !IS_ERR(inode) && generation != 0 &&
  1494. generation != inode->i_generation) {
  1495. iput(inode);
  1496. inode = NULL;
  1497. }
  1498. reiserfs_write_unlock(sb);
  1499. return d_obtain_alias(inode);
  1500. }
  1501. struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1502. int fh_len, int fh_type)
  1503. {
  1504. /*
  1505. * fhtype happens to reflect the number of u32s encoded.
  1506. * due to a bug in earlier code, fhtype might indicate there
  1507. * are more u32s then actually fitted.
  1508. * so if fhtype seems to be more than len, reduce fhtype.
  1509. * Valid types are:
  1510. * 2 - objectid + dir_id - legacy support
  1511. * 3 - objectid + dir_id + generation
  1512. * 4 - objectid + dir_id + objectid and dirid of parent - legacy
  1513. * 5 - objectid + dir_id + generation + objectid and dirid of parent
  1514. * 6 - as above plus generation of directory
  1515. * 6 does not fit in NFSv2 handles
  1516. */
  1517. if (fh_type > fh_len) {
  1518. if (fh_type != 6 || fh_len != 5)
  1519. reiserfs_warning(sb, "reiserfs-13077",
  1520. "nfsd/reiserfs, fhtype=%d, len=%d - odd",
  1521. fh_type, fh_len);
  1522. fh_type = fh_len;
  1523. }
  1524. if (fh_len < 2)
  1525. return NULL;
  1526. return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
  1527. (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
  1528. }
  1529. struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1530. int fh_len, int fh_type)
  1531. {
  1532. if (fh_type > fh_len)
  1533. fh_type = fh_len;
  1534. if (fh_type < 4)
  1535. return NULL;
  1536. return reiserfs_get_dentry(sb,
  1537. (fh_type >= 5) ? fid->raw[3] : fid->raw[2],
  1538. (fh_type >= 5) ? fid->raw[4] : fid->raw[3],
  1539. (fh_type == 6) ? fid->raw[5] : 0);
  1540. }
  1541. int reiserfs_encode_fh(struct inode *inode, __u32 * data, int *lenp,
  1542. struct inode *parent)
  1543. {
  1544. int maxlen = *lenp;
  1545. if (parent && (maxlen < 5)) {
  1546. *lenp = 5;
  1547. return FILEID_INVALID;
  1548. } else if (maxlen < 3) {
  1549. *lenp = 3;
  1550. return FILEID_INVALID;
  1551. }
  1552. data[0] = inode->i_ino;
  1553. data[1] = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1554. data[2] = inode->i_generation;
  1555. *lenp = 3;
  1556. if (parent) {
  1557. data[3] = parent->i_ino;
  1558. data[4] = le32_to_cpu(INODE_PKEY(parent)->k_dir_id);
  1559. *lenp = 5;
  1560. if (maxlen >= 6) {
  1561. data[5] = parent->i_generation;
  1562. *lenp = 6;
  1563. }
  1564. }
  1565. return *lenp;
  1566. }
  1567. /*
  1568. * looks for stat data, then copies fields to it, marks the buffer
  1569. * containing stat data as dirty
  1570. */
  1571. /*
  1572. * reiserfs inodes are never really dirty, since the dirty inode call
  1573. * always logs them. This call allows the VFS inode marking routines
  1574. * to properly mark inodes for datasync and such, but only actually
  1575. * does something when called for a synchronous update.
  1576. */
  1577. int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1578. {
  1579. struct reiserfs_transaction_handle th;
  1580. int jbegin_count = 1;
  1581. if (sb_rdonly(inode->i_sb))
  1582. return -EROFS;
  1583. /*
  1584. * memory pressure can sometimes initiate write_inode calls with
  1585. * sync == 1,
  1586. * these cases are just when the system needs ram, not when the
  1587. * inode needs to reach disk for safety, and they can safely be
  1588. * ignored because the altered inode has already been logged.
  1589. */
  1590. if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
  1591. reiserfs_write_lock(inode->i_sb);
  1592. if (!journal_begin(&th, inode->i_sb, jbegin_count)) {
  1593. reiserfs_update_sd(&th, inode);
  1594. journal_end_sync(&th);
  1595. }
  1596. reiserfs_write_unlock(inode->i_sb);
  1597. }
  1598. return 0;
  1599. }
  1600. /*
  1601. * stat data of new object is inserted already, this inserts the item
  1602. * containing "." and ".." entries
  1603. */
  1604. static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
  1605. struct inode *inode,
  1606. struct item_head *ih, struct treepath *path,
  1607. struct inode *dir)
  1608. {
  1609. struct super_block *sb = th->t_super;
  1610. char empty_dir[EMPTY_DIR_SIZE];
  1611. char *body = empty_dir;
  1612. struct cpu_key key;
  1613. int retval;
  1614. BUG_ON(!th->t_trans_id);
  1615. _make_cpu_key(&key, KEY_FORMAT_3_5, le32_to_cpu(ih->ih_key.k_dir_id),
  1616. le32_to_cpu(ih->ih_key.k_objectid), DOT_OFFSET,
  1617. TYPE_DIRENTRY, 3 /*key length */ );
  1618. /*
  1619. * compose item head for new item. Directories consist of items of
  1620. * old type (ITEM_VERSION_1). Do not set key (second arg is 0), it
  1621. * is done by reiserfs_new_inode
  1622. */
  1623. if (old_format_only(sb)) {
  1624. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
  1625. TYPE_DIRENTRY, EMPTY_DIR_SIZE_V1, 2);
  1626. make_empty_dir_item_v1(body, ih->ih_key.k_dir_id,
  1627. ih->ih_key.k_objectid,
  1628. INODE_PKEY(dir)->k_dir_id,
  1629. INODE_PKEY(dir)->k_objectid);
  1630. } else {
  1631. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
  1632. TYPE_DIRENTRY, EMPTY_DIR_SIZE, 2);
  1633. make_empty_dir_item(body, ih->ih_key.k_dir_id,
  1634. ih->ih_key.k_objectid,
  1635. INODE_PKEY(dir)->k_dir_id,
  1636. INODE_PKEY(dir)->k_objectid);
  1637. }
  1638. /* look for place in the tree for new item */
  1639. retval = search_item(sb, &key, path);
  1640. if (retval == IO_ERROR) {
  1641. reiserfs_error(sb, "vs-13080",
  1642. "i/o failure occurred creating new directory");
  1643. return -EIO;
  1644. }
  1645. if (retval == ITEM_FOUND) {
  1646. pathrelse(path);
  1647. reiserfs_warning(sb, "vs-13070",
  1648. "object with this key exists (%k)",
  1649. &(ih->ih_key));
  1650. return -EEXIST;
  1651. }
  1652. /* insert item, that is empty directory item */
  1653. return reiserfs_insert_item(th, path, &key, ih, inode, body);
  1654. }
  1655. /*
  1656. * stat data of object has been inserted, this inserts the item
  1657. * containing the body of symlink
  1658. */
  1659. static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th,
  1660. struct inode *inode,
  1661. struct item_head *ih,
  1662. struct treepath *path, const char *symname,
  1663. int item_len)
  1664. {
  1665. struct super_block *sb = th->t_super;
  1666. struct cpu_key key;
  1667. int retval;
  1668. BUG_ON(!th->t_trans_id);
  1669. _make_cpu_key(&key, KEY_FORMAT_3_5,
  1670. le32_to_cpu(ih->ih_key.k_dir_id),
  1671. le32_to_cpu(ih->ih_key.k_objectid),
  1672. 1, TYPE_DIRECT, 3 /*key length */ );
  1673. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
  1674. 0 /*free_space */ );
  1675. /* look for place in the tree for new item */
  1676. retval = search_item(sb, &key, path);
  1677. if (retval == IO_ERROR) {
  1678. reiserfs_error(sb, "vs-13080",
  1679. "i/o failure occurred creating new symlink");
  1680. return -EIO;
  1681. }
  1682. if (retval == ITEM_FOUND) {
  1683. pathrelse(path);
  1684. reiserfs_warning(sb, "vs-13080",
  1685. "object with this key exists (%k)",
  1686. &(ih->ih_key));
  1687. return -EEXIST;
  1688. }
  1689. /* insert item, that is body of symlink */
  1690. return reiserfs_insert_item(th, path, &key, ih, inode, symname);
  1691. }
  1692. /*
  1693. * inserts the stat data into the tree, and then calls
  1694. * reiserfs_new_directory (to insert ".", ".." item if new object is
  1695. * directory) or reiserfs_new_symlink (to insert symlink body if new
  1696. * object is symlink) or nothing (if new object is regular file)
  1697. * NOTE! uid and gid must already be set in the inode. If we return
  1698. * non-zero due to an error, we have to drop the quota previously allocated
  1699. * for the fresh inode. This can only be done outside a transaction, so
  1700. * if we return non-zero, we also end the transaction.
  1701. *
  1702. * @th: active transaction handle
  1703. * @dir: parent directory for new inode
  1704. * @mode: mode of new inode
  1705. * @symname: symlink contents if inode is symlink
  1706. * @isize: 0 for regular file, EMPTY_DIR_SIZE for dirs, strlen(symname) for
  1707. * symlinks
  1708. * @inode: inode to be filled
  1709. * @security: optional security context to associate with this inode
  1710. */
  1711. int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
  1712. struct inode *dir, umode_t mode, const char *symname,
  1713. /* 0 for regular, EMTRY_DIR_SIZE for dirs,
  1714. strlen (symname) for symlinks) */
  1715. loff_t i_size, struct dentry *dentry,
  1716. struct inode *inode,
  1717. struct reiserfs_security_handle *security)
  1718. {
  1719. struct super_block *sb = dir->i_sb;
  1720. struct reiserfs_iget_args args;
  1721. INITIALIZE_PATH(path_to_key);
  1722. struct cpu_key key;
  1723. struct item_head ih;
  1724. struct stat_data sd;
  1725. int retval;
  1726. int err;
  1727. int depth;
  1728. BUG_ON(!th->t_trans_id);
  1729. depth = reiserfs_write_unlock_nested(sb);
  1730. err = dquot_alloc_inode(inode);
  1731. reiserfs_write_lock_nested(sb, depth);
  1732. if (err)
  1733. goto out_end_trans;
  1734. if (!dir->i_nlink) {
  1735. err = -EPERM;
  1736. goto out_bad_inode;
  1737. }
  1738. /* item head of new item */
  1739. ih.ih_key.k_dir_id = reiserfs_choose_packing(dir);
  1740. ih.ih_key.k_objectid = cpu_to_le32(reiserfs_get_unused_objectid(th));
  1741. if (!ih.ih_key.k_objectid) {
  1742. err = -ENOMEM;
  1743. goto out_bad_inode;
  1744. }
  1745. args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
  1746. if (old_format_only(sb))
  1747. make_le_item_head(&ih, NULL, KEY_FORMAT_3_5, SD_OFFSET,
  1748. TYPE_STAT_DATA, SD_V1_SIZE, MAX_US_INT);
  1749. else
  1750. make_le_item_head(&ih, NULL, KEY_FORMAT_3_6, SD_OFFSET,
  1751. TYPE_STAT_DATA, SD_SIZE, MAX_US_INT);
  1752. memcpy(INODE_PKEY(inode), &ih.ih_key, KEY_SIZE);
  1753. args.dirid = le32_to_cpu(ih.ih_key.k_dir_id);
  1754. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1755. err = insert_inode_locked4(inode, args.objectid,
  1756. reiserfs_find_actor, &args);
  1757. reiserfs_write_lock_nested(inode->i_sb, depth);
  1758. if (err) {
  1759. err = -EINVAL;
  1760. goto out_bad_inode;
  1761. }
  1762. if (old_format_only(sb))
  1763. /*
  1764. * not a perfect generation count, as object ids can be reused,
  1765. * but this is as good as reiserfs can do right now.
  1766. * note that the private part of inode isn't filled in yet,
  1767. * we have to use the directory.
  1768. */
  1769. inode->i_generation = le32_to_cpu(INODE_PKEY(dir)->k_objectid);
  1770. else
  1771. #if defined( USE_INODE_GENERATION_COUNTER )
  1772. inode->i_generation =
  1773. le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
  1774. #else
  1775. inode->i_generation = ++event;
  1776. #endif
  1777. /* fill stat data */
  1778. set_nlink(inode, (S_ISDIR(mode) ? 2 : 1));
  1779. /* uid and gid must already be set by the caller for quota init */
  1780. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  1781. inode->i_size = i_size;
  1782. inode->i_blocks = 0;
  1783. inode->i_bytes = 0;
  1784. REISERFS_I(inode)->i_first_direct_byte = S_ISLNK(mode) ? 1 :
  1785. U32_MAX /*NO_BYTES_IN_DIRECT_ITEM */ ;
  1786. INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
  1787. REISERFS_I(inode)->i_flags = 0;
  1788. REISERFS_I(inode)->i_prealloc_block = 0;
  1789. REISERFS_I(inode)->i_prealloc_count = 0;
  1790. REISERFS_I(inode)->i_trans_id = 0;
  1791. REISERFS_I(inode)->i_jl = NULL;
  1792. REISERFS_I(inode)->i_attrs =
  1793. REISERFS_I(dir)->i_attrs & REISERFS_INHERIT_MASK;
  1794. sd_attrs_to_i_attrs(REISERFS_I(inode)->i_attrs, inode);
  1795. reiserfs_init_xattr_rwsem(inode);
  1796. /* key to search for correct place for new stat data */
  1797. _make_cpu_key(&key, KEY_FORMAT_3_6, le32_to_cpu(ih.ih_key.k_dir_id),
  1798. le32_to_cpu(ih.ih_key.k_objectid), SD_OFFSET,
  1799. TYPE_STAT_DATA, 3 /*key length */ );
  1800. /* find proper place for inserting of stat data */
  1801. retval = search_item(sb, &key, &path_to_key);
  1802. if (retval == IO_ERROR) {
  1803. err = -EIO;
  1804. goto out_bad_inode;
  1805. }
  1806. if (retval == ITEM_FOUND) {
  1807. pathrelse(&path_to_key);
  1808. err = -EEXIST;
  1809. goto out_bad_inode;
  1810. }
  1811. if (old_format_only(sb)) {
  1812. /* i_uid or i_gid is too big to be stored in stat data v3.5 */
  1813. if (i_uid_read(inode) & ~0xffff || i_gid_read(inode) & ~0xffff) {
  1814. pathrelse(&path_to_key);
  1815. err = -EINVAL;
  1816. goto out_bad_inode;
  1817. }
  1818. inode2sd_v1(&sd, inode, inode->i_size);
  1819. } else {
  1820. inode2sd(&sd, inode, inode->i_size);
  1821. }
  1822. /*
  1823. * store in in-core inode the key of stat data and version all
  1824. * object items will have (directory items will have old offset
  1825. * format, other new objects will consist of new items)
  1826. */
  1827. if (old_format_only(sb) || S_ISDIR(mode) || S_ISLNK(mode))
  1828. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1829. else
  1830. set_inode_item_key_version(inode, KEY_FORMAT_3_6);
  1831. if (old_format_only(sb))
  1832. set_inode_sd_version(inode, STAT_DATA_V1);
  1833. else
  1834. set_inode_sd_version(inode, STAT_DATA_V2);
  1835. /* insert the stat data into the tree */
  1836. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1837. if (REISERFS_I(dir)->new_packing_locality)
  1838. th->displace_new_blocks = 1;
  1839. #endif
  1840. retval =
  1841. reiserfs_insert_item(th, &path_to_key, &key, &ih, inode,
  1842. (char *)(&sd));
  1843. if (retval) {
  1844. err = retval;
  1845. reiserfs_check_path(&path_to_key);
  1846. goto out_bad_inode;
  1847. }
  1848. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1849. if (!th->displace_new_blocks)
  1850. REISERFS_I(dir)->new_packing_locality = 0;
  1851. #endif
  1852. if (S_ISDIR(mode)) {
  1853. /* insert item with "." and ".." */
  1854. retval =
  1855. reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
  1856. }
  1857. if (S_ISLNK(mode)) {
  1858. /* insert body of symlink */
  1859. if (!old_format_only(sb))
  1860. i_size = ROUND_UP(i_size);
  1861. retval =
  1862. reiserfs_new_symlink(th, inode, &ih, &path_to_key, symname,
  1863. i_size);
  1864. }
  1865. if (retval) {
  1866. err = retval;
  1867. reiserfs_check_path(&path_to_key);
  1868. journal_end(th);
  1869. goto out_inserted_sd;
  1870. }
  1871. /*
  1872. * Mark it private if we're creating the privroot
  1873. * or something under it.
  1874. */
  1875. if (IS_PRIVATE(dir) || dentry == REISERFS_SB(sb)->priv_root) {
  1876. inode->i_flags |= S_PRIVATE;
  1877. inode->i_opflags &= ~IOP_XATTR;
  1878. }
  1879. if (reiserfs_posixacl(inode->i_sb)) {
  1880. reiserfs_write_unlock(inode->i_sb);
  1881. retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
  1882. reiserfs_write_lock(inode->i_sb);
  1883. if (retval) {
  1884. err = retval;
  1885. reiserfs_check_path(&path_to_key);
  1886. journal_end(th);
  1887. goto out_inserted_sd;
  1888. }
  1889. } else if (inode->i_sb->s_flags & SB_POSIXACL) {
  1890. reiserfs_warning(inode->i_sb, "jdm-13090",
  1891. "ACLs aren't enabled in the fs, "
  1892. "but vfs thinks they are!");
  1893. }
  1894. if (security->name) {
  1895. reiserfs_write_unlock(inode->i_sb);
  1896. retval = reiserfs_security_write(th, inode, security);
  1897. reiserfs_write_lock(inode->i_sb);
  1898. if (retval) {
  1899. err = retval;
  1900. reiserfs_check_path(&path_to_key);
  1901. retval = journal_end(th);
  1902. if (retval)
  1903. err = retval;
  1904. goto out_inserted_sd;
  1905. }
  1906. }
  1907. reiserfs_update_sd(th, inode);
  1908. reiserfs_check_path(&path_to_key);
  1909. return 0;
  1910. out_bad_inode:
  1911. /* Invalidate the object, nothing was inserted yet */
  1912. INODE_PKEY(inode)->k_objectid = 0;
  1913. /* Quota change must be inside a transaction for journaling */
  1914. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1915. dquot_free_inode(inode);
  1916. reiserfs_write_lock_nested(inode->i_sb, depth);
  1917. out_end_trans:
  1918. journal_end(th);
  1919. /*
  1920. * Drop can be outside and it needs more credits so it's better
  1921. * to have it outside
  1922. */
  1923. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1924. dquot_drop(inode);
  1925. reiserfs_write_lock_nested(inode->i_sb, depth);
  1926. inode->i_flags |= S_NOQUOTA;
  1927. make_bad_inode(inode);
  1928. out_inserted_sd:
  1929. clear_nlink(inode);
  1930. th->t_trans_id = 0; /* so the caller can't use this handle later */
  1931. if (inode->i_state & I_NEW)
  1932. unlock_new_inode(inode);
  1933. iput(inode);
  1934. return err;
  1935. }
  1936. /*
  1937. * finds the tail page in the page cache,
  1938. * reads the last block in.
  1939. *
  1940. * On success, page_result is set to a locked, pinned page, and bh_result
  1941. * is set to an up to date buffer for the last block in the file. returns 0.
  1942. *
  1943. * tail conversion is not done, so bh_result might not be valid for writing
  1944. * check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
  1945. * trying to write the block.
  1946. *
  1947. * on failure, nonzero is returned, page_result and bh_result are untouched.
  1948. */
  1949. static int grab_tail_page(struct inode *inode,
  1950. struct page **page_result,
  1951. struct buffer_head **bh_result)
  1952. {
  1953. /*
  1954. * we want the page with the last byte in the file,
  1955. * not the page that will hold the next byte for appending
  1956. */
  1957. unsigned long index = (inode->i_size - 1) >> PAGE_SHIFT;
  1958. unsigned long pos = 0;
  1959. unsigned long start = 0;
  1960. unsigned long blocksize = inode->i_sb->s_blocksize;
  1961. unsigned long offset = (inode->i_size) & (PAGE_SIZE - 1);
  1962. struct buffer_head *bh;
  1963. struct buffer_head *head;
  1964. struct page *page;
  1965. int error;
  1966. /*
  1967. * we know that we are only called with inode->i_size > 0.
  1968. * we also know that a file tail can never be as big as a block
  1969. * If i_size % blocksize == 0, our file is currently block aligned
  1970. * and it won't need converting or zeroing after a truncate.
  1971. */
  1972. if ((offset & (blocksize - 1)) == 0) {
  1973. return -ENOENT;
  1974. }
  1975. page = grab_cache_page(inode->i_mapping, index);
  1976. error = -ENOMEM;
  1977. if (!page) {
  1978. goto out;
  1979. }
  1980. /* start within the page of the last block in the file */
  1981. start = (offset / blocksize) * blocksize;
  1982. error = __block_write_begin(page, start, offset - start,
  1983. reiserfs_get_block_create_0);
  1984. if (error)
  1985. goto unlock;
  1986. head = page_buffers(page);
  1987. bh = head;
  1988. do {
  1989. if (pos >= start) {
  1990. break;
  1991. }
  1992. bh = bh->b_this_page;
  1993. pos += blocksize;
  1994. } while (bh != head);
  1995. if (!buffer_uptodate(bh)) {
  1996. /*
  1997. * note, this should never happen, prepare_write should be
  1998. * taking care of this for us. If the buffer isn't up to
  1999. * date, I've screwed up the code to find the buffer, or the
  2000. * code to call prepare_write
  2001. */
  2002. reiserfs_error(inode->i_sb, "clm-6000",
  2003. "error reading block %lu", bh->b_blocknr);
  2004. error = -EIO;
  2005. goto unlock;
  2006. }
  2007. *bh_result = bh;
  2008. *page_result = page;
  2009. out:
  2010. return error;
  2011. unlock:
  2012. unlock_page(page);
  2013. put_page(page);
  2014. return error;
  2015. }
  2016. /*
  2017. * vfs version of truncate file. Must NOT be called with
  2018. * a transaction already started.
  2019. *
  2020. * some code taken from block_truncate_page
  2021. */
  2022. int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
  2023. {
  2024. struct reiserfs_transaction_handle th;
  2025. /* we want the offset for the first byte after the end of the file */
  2026. unsigned long offset = inode->i_size & (PAGE_SIZE - 1);
  2027. unsigned blocksize = inode->i_sb->s_blocksize;
  2028. unsigned length;
  2029. struct page *page = NULL;
  2030. int error;
  2031. struct buffer_head *bh = NULL;
  2032. int err2;
  2033. reiserfs_write_lock(inode->i_sb);
  2034. if (inode->i_size > 0) {
  2035. error = grab_tail_page(inode, &page, &bh);
  2036. if (error) {
  2037. /*
  2038. * -ENOENT means we truncated past the end of the
  2039. * file, and get_block_create_0 could not find a
  2040. * block to read in, which is ok.
  2041. */
  2042. if (error != -ENOENT)
  2043. reiserfs_error(inode->i_sb, "clm-6001",
  2044. "grab_tail_page failed %d",
  2045. error);
  2046. page = NULL;
  2047. bh = NULL;
  2048. }
  2049. }
  2050. /*
  2051. * so, if page != NULL, we have a buffer head for the offset at
  2052. * the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
  2053. * then we have an unformatted node. Otherwise, we have a direct item,
  2054. * and no zeroing is required on disk. We zero after the truncate,
  2055. * because the truncate might pack the item anyway
  2056. * (it will unmap bh if it packs).
  2057. *
  2058. * it is enough to reserve space in transaction for 2 balancings:
  2059. * one for "save" link adding and another for the first
  2060. * cut_from_item. 1 is for update_sd
  2061. */
  2062. error = journal_begin(&th, inode->i_sb,
  2063. JOURNAL_PER_BALANCE_CNT * 2 + 1);
  2064. if (error)
  2065. goto out;
  2066. reiserfs_update_inode_transaction(inode);
  2067. if (update_timestamps)
  2068. /*
  2069. * we are doing real truncate: if the system crashes
  2070. * before the last transaction of truncating gets committed
  2071. * - on reboot the file either appears truncated properly
  2072. * or not truncated at all
  2073. */
  2074. add_save_link(&th, inode, 1);
  2075. err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
  2076. error = journal_end(&th);
  2077. if (error)
  2078. goto out;
  2079. /* check reiserfs_do_truncate after ending the transaction */
  2080. if (err2) {
  2081. error = err2;
  2082. goto out;
  2083. }
  2084. if (update_timestamps) {
  2085. error = remove_save_link(inode, 1 /* truncate */);
  2086. if (error)
  2087. goto out;
  2088. }
  2089. if (page) {
  2090. length = offset & (blocksize - 1);
  2091. /* if we are not on a block boundary */
  2092. if (length) {
  2093. length = blocksize - length;
  2094. zero_user(page, offset, length);
  2095. if (buffer_mapped(bh) && bh->b_blocknr != 0) {
  2096. mark_buffer_dirty(bh);
  2097. }
  2098. }
  2099. unlock_page(page);
  2100. put_page(page);
  2101. }
  2102. reiserfs_write_unlock(inode->i_sb);
  2103. return 0;
  2104. out:
  2105. if (page) {
  2106. unlock_page(page);
  2107. put_page(page);
  2108. }
  2109. reiserfs_write_unlock(inode->i_sb);
  2110. return error;
  2111. }
  2112. static int map_block_for_writepage(struct inode *inode,
  2113. struct buffer_head *bh_result,
  2114. unsigned long block)
  2115. {
  2116. struct reiserfs_transaction_handle th;
  2117. int fs_gen;
  2118. struct item_head tmp_ih;
  2119. struct item_head *ih;
  2120. struct buffer_head *bh;
  2121. __le32 *item;
  2122. struct cpu_key key;
  2123. INITIALIZE_PATH(path);
  2124. int pos_in_item;
  2125. int jbegin_count = JOURNAL_PER_BALANCE_CNT;
  2126. loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
  2127. int retval;
  2128. int use_get_block = 0;
  2129. int bytes_copied = 0;
  2130. int copy_size;
  2131. int trans_running = 0;
  2132. /*
  2133. * catch places below that try to log something without
  2134. * starting a trans
  2135. */
  2136. th.t_trans_id = 0;
  2137. if (!buffer_uptodate(bh_result)) {
  2138. return -EIO;
  2139. }
  2140. kmap(bh_result->b_page);
  2141. start_over:
  2142. reiserfs_write_lock(inode->i_sb);
  2143. make_cpu_key(&key, inode, byte_offset, TYPE_ANY, 3);
  2144. research:
  2145. retval = search_for_position_by_key(inode->i_sb, &key, &path);
  2146. if (retval != POSITION_FOUND) {
  2147. use_get_block = 1;
  2148. goto out;
  2149. }
  2150. bh = get_last_bh(&path);
  2151. ih = tp_item_head(&path);
  2152. item = tp_item_body(&path);
  2153. pos_in_item = path.pos_in_item;
  2154. /* we've found an unformatted node */
  2155. if (indirect_item_found(retval, ih)) {
  2156. if (bytes_copied > 0) {
  2157. reiserfs_warning(inode->i_sb, "clm-6002",
  2158. "bytes_copied %d", bytes_copied);
  2159. }
  2160. if (!get_block_num(item, pos_in_item)) {
  2161. /* crap, we are writing to a hole */
  2162. use_get_block = 1;
  2163. goto out;
  2164. }
  2165. set_block_dev_mapped(bh_result,
  2166. get_block_num(item, pos_in_item), inode);
  2167. } else if (is_direct_le_ih(ih)) {
  2168. char *p;
  2169. p = page_address(bh_result->b_page);
  2170. p += (byte_offset - 1) & (PAGE_SIZE - 1);
  2171. copy_size = ih_item_len(ih) - pos_in_item;
  2172. fs_gen = get_generation(inode->i_sb);
  2173. copy_item_head(&tmp_ih, ih);
  2174. if (!trans_running) {
  2175. /* vs-3050 is gone, no need to drop the path */
  2176. retval = journal_begin(&th, inode->i_sb, jbegin_count);
  2177. if (retval)
  2178. goto out;
  2179. reiserfs_update_inode_transaction(inode);
  2180. trans_running = 1;
  2181. if (fs_changed(fs_gen, inode->i_sb)
  2182. && item_moved(&tmp_ih, &path)) {
  2183. reiserfs_restore_prepared_buffer(inode->i_sb,
  2184. bh);
  2185. goto research;
  2186. }
  2187. }
  2188. reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
  2189. if (fs_changed(fs_gen, inode->i_sb)
  2190. && item_moved(&tmp_ih, &path)) {
  2191. reiserfs_restore_prepared_buffer(inode->i_sb, bh);
  2192. goto research;
  2193. }
  2194. memcpy(ih_item_body(bh, ih) + pos_in_item, p + bytes_copied,
  2195. copy_size);
  2196. journal_mark_dirty(&th, bh);
  2197. bytes_copied += copy_size;
  2198. set_block_dev_mapped(bh_result, 0, inode);
  2199. /* are there still bytes left? */
  2200. if (bytes_copied < bh_result->b_size &&
  2201. (byte_offset + bytes_copied) < inode->i_size) {
  2202. set_cpu_key_k_offset(&key,
  2203. cpu_key_k_offset(&key) +
  2204. copy_size);
  2205. goto research;
  2206. }
  2207. } else {
  2208. reiserfs_warning(inode->i_sb, "clm-6003",
  2209. "bad item inode %lu", inode->i_ino);
  2210. retval = -EIO;
  2211. goto out;
  2212. }
  2213. retval = 0;
  2214. out:
  2215. pathrelse(&path);
  2216. if (trans_running) {
  2217. int err = journal_end(&th);
  2218. if (err)
  2219. retval = err;
  2220. trans_running = 0;
  2221. }
  2222. reiserfs_write_unlock(inode->i_sb);
  2223. /* this is where we fill in holes in the file. */
  2224. if (use_get_block) {
  2225. retval = reiserfs_get_block(inode, block, bh_result,
  2226. GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
  2227. | GET_BLOCK_NO_DANGLE);
  2228. if (!retval) {
  2229. if (!buffer_mapped(bh_result)
  2230. || bh_result->b_blocknr == 0) {
  2231. /* get_block failed to find a mapped unformatted node. */
  2232. use_get_block = 0;
  2233. goto start_over;
  2234. }
  2235. }
  2236. }
  2237. kunmap(bh_result->b_page);
  2238. if (!retval && buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
  2239. /*
  2240. * we've copied data from the page into the direct item, so the
  2241. * buffer in the page is now clean, mark it to reflect that.
  2242. */
  2243. lock_buffer(bh_result);
  2244. clear_buffer_dirty(bh_result);
  2245. unlock_buffer(bh_result);
  2246. }
  2247. return retval;
  2248. }
  2249. /*
  2250. * [email protected]: updated in 2.5.54 to follow the same general io
  2251. * start/recovery path as __block_write_full_page, along with special
  2252. * code to handle reiserfs tails.
  2253. */
  2254. static int reiserfs_write_full_page(struct page *page,
  2255. struct writeback_control *wbc)
  2256. {
  2257. struct inode *inode = page->mapping->host;
  2258. unsigned long end_index = inode->i_size >> PAGE_SHIFT;
  2259. int error = 0;
  2260. unsigned long block;
  2261. sector_t last_block;
  2262. struct buffer_head *head, *bh;
  2263. int partial = 0;
  2264. int nr = 0;
  2265. int checked = PageChecked(page);
  2266. struct reiserfs_transaction_handle th;
  2267. struct super_block *s = inode->i_sb;
  2268. int bh_per_page = PAGE_SIZE / s->s_blocksize;
  2269. th.t_trans_id = 0;
  2270. /* no logging allowed when nonblocking or from PF_MEMALLOC */
  2271. if (checked && (current->flags & PF_MEMALLOC)) {
  2272. redirty_page_for_writepage(wbc, page);
  2273. unlock_page(page);
  2274. return 0;
  2275. }
  2276. /*
  2277. * The page dirty bit is cleared before writepage is called, which
  2278. * means we have to tell create_empty_buffers to make dirty buffers
  2279. * The page really should be up to date at this point, so tossing
  2280. * in the BH_Uptodate is just a sanity check.
  2281. */
  2282. if (!page_has_buffers(page)) {
  2283. create_empty_buffers(page, s->s_blocksize,
  2284. (1 << BH_Dirty) | (1 << BH_Uptodate));
  2285. }
  2286. head = page_buffers(page);
  2287. /*
  2288. * last page in the file, zero out any contents past the
  2289. * last byte in the file
  2290. */
  2291. if (page->index >= end_index) {
  2292. unsigned last_offset;
  2293. last_offset = inode->i_size & (PAGE_SIZE - 1);
  2294. /* no file contents in this page */
  2295. if (page->index >= end_index + 1 || !last_offset) {
  2296. unlock_page(page);
  2297. return 0;
  2298. }
  2299. zero_user_segment(page, last_offset, PAGE_SIZE);
  2300. }
  2301. bh = head;
  2302. block = page->index << (PAGE_SHIFT - s->s_blocksize_bits);
  2303. last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
  2304. /* first map all the buffers, logging any direct items we find */
  2305. do {
  2306. if (block > last_block) {
  2307. /*
  2308. * This can happen when the block size is less than
  2309. * the page size. The corresponding bytes in the page
  2310. * were zero filled above
  2311. */
  2312. clear_buffer_dirty(bh);
  2313. set_buffer_uptodate(bh);
  2314. } else if ((checked || buffer_dirty(bh)) &&
  2315. (!buffer_mapped(bh) || bh->b_blocknr == 0)) {
  2316. /*
  2317. * not mapped yet, or it points to a direct item, search
  2318. * the btree for the mapping info, and log any direct
  2319. * items found
  2320. */
  2321. if ((error = map_block_for_writepage(inode, bh, block))) {
  2322. goto fail;
  2323. }
  2324. }
  2325. bh = bh->b_this_page;
  2326. block++;
  2327. } while (bh != head);
  2328. /*
  2329. * we start the transaction after map_block_for_writepage,
  2330. * because it can create holes in the file (an unbounded operation).
  2331. * starting it here, we can make a reliable estimate for how many
  2332. * blocks we're going to log
  2333. */
  2334. if (checked) {
  2335. ClearPageChecked(page);
  2336. reiserfs_write_lock(s);
  2337. error = journal_begin(&th, s, bh_per_page + 1);
  2338. if (error) {
  2339. reiserfs_write_unlock(s);
  2340. goto fail;
  2341. }
  2342. reiserfs_update_inode_transaction(inode);
  2343. }
  2344. /* now go through and lock any dirty buffers on the page */
  2345. do {
  2346. get_bh(bh);
  2347. if (!buffer_mapped(bh))
  2348. continue;
  2349. if (buffer_mapped(bh) && bh->b_blocknr == 0)
  2350. continue;
  2351. if (checked) {
  2352. reiserfs_prepare_for_journal(s, bh, 1);
  2353. journal_mark_dirty(&th, bh);
  2354. continue;
  2355. }
  2356. /*
  2357. * from this point on, we know the buffer is mapped to a
  2358. * real block and not a direct item
  2359. */
  2360. if (wbc->sync_mode != WB_SYNC_NONE) {
  2361. lock_buffer(bh);
  2362. } else {
  2363. if (!trylock_buffer(bh)) {
  2364. redirty_page_for_writepage(wbc, page);
  2365. continue;
  2366. }
  2367. }
  2368. if (test_clear_buffer_dirty(bh)) {
  2369. mark_buffer_async_write(bh);
  2370. } else {
  2371. unlock_buffer(bh);
  2372. }
  2373. } while ((bh = bh->b_this_page) != head);
  2374. if (checked) {
  2375. error = journal_end(&th);
  2376. reiserfs_write_unlock(s);
  2377. if (error)
  2378. goto fail;
  2379. }
  2380. BUG_ON(PageWriteback(page));
  2381. set_page_writeback(page);
  2382. unlock_page(page);
  2383. /*
  2384. * since any buffer might be the only dirty buffer on the page,
  2385. * the first submit_bh can bring the page out of writeback.
  2386. * be careful with the buffers.
  2387. */
  2388. do {
  2389. struct buffer_head *next = bh->b_this_page;
  2390. if (buffer_async_write(bh)) {
  2391. submit_bh(REQ_OP_WRITE, bh);
  2392. nr++;
  2393. }
  2394. put_bh(bh);
  2395. bh = next;
  2396. } while (bh != head);
  2397. error = 0;
  2398. done:
  2399. if (nr == 0) {
  2400. /*
  2401. * if this page only had a direct item, it is very possible for
  2402. * no io to be required without there being an error. Or,
  2403. * someone else could have locked them and sent them down the
  2404. * pipe without locking the page
  2405. */
  2406. bh = head;
  2407. do {
  2408. if (!buffer_uptodate(bh)) {
  2409. partial = 1;
  2410. break;
  2411. }
  2412. bh = bh->b_this_page;
  2413. } while (bh != head);
  2414. if (!partial)
  2415. SetPageUptodate(page);
  2416. end_page_writeback(page);
  2417. }
  2418. return error;
  2419. fail:
  2420. /*
  2421. * catches various errors, we need to make sure any valid dirty blocks
  2422. * get to the media. The page is currently locked and not marked for
  2423. * writeback
  2424. */
  2425. ClearPageUptodate(page);
  2426. bh = head;
  2427. do {
  2428. get_bh(bh);
  2429. if (buffer_mapped(bh) && buffer_dirty(bh) && bh->b_blocknr) {
  2430. lock_buffer(bh);
  2431. mark_buffer_async_write(bh);
  2432. } else {
  2433. /*
  2434. * clear any dirty bits that might have come from
  2435. * getting attached to a dirty page
  2436. */
  2437. clear_buffer_dirty(bh);
  2438. }
  2439. bh = bh->b_this_page;
  2440. } while (bh != head);
  2441. SetPageError(page);
  2442. BUG_ON(PageWriteback(page));
  2443. set_page_writeback(page);
  2444. unlock_page(page);
  2445. do {
  2446. struct buffer_head *next = bh->b_this_page;
  2447. if (buffer_async_write(bh)) {
  2448. clear_buffer_dirty(bh);
  2449. submit_bh(REQ_OP_WRITE, bh);
  2450. nr++;
  2451. }
  2452. put_bh(bh);
  2453. bh = next;
  2454. } while (bh != head);
  2455. goto done;
  2456. }
  2457. static int reiserfs_read_folio(struct file *f, struct folio *folio)
  2458. {
  2459. return block_read_full_folio(folio, reiserfs_get_block);
  2460. }
  2461. static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
  2462. {
  2463. struct inode *inode = page->mapping->host;
  2464. reiserfs_wait_on_write_block(inode->i_sb);
  2465. return reiserfs_write_full_page(page, wbc);
  2466. }
  2467. static void reiserfs_truncate_failed_write(struct inode *inode)
  2468. {
  2469. truncate_inode_pages(inode->i_mapping, inode->i_size);
  2470. reiserfs_truncate_file(inode, 0);
  2471. }
  2472. static int reiserfs_write_begin(struct file *file,
  2473. struct address_space *mapping,
  2474. loff_t pos, unsigned len,
  2475. struct page **pagep, void **fsdata)
  2476. {
  2477. struct inode *inode;
  2478. struct page *page;
  2479. pgoff_t index;
  2480. int ret;
  2481. int old_ref = 0;
  2482. inode = mapping->host;
  2483. index = pos >> PAGE_SHIFT;
  2484. page = grab_cache_page_write_begin(mapping, index);
  2485. if (!page)
  2486. return -ENOMEM;
  2487. *pagep = page;
  2488. reiserfs_wait_on_write_block(inode->i_sb);
  2489. fix_tail_page_for_writing(page);
  2490. if (reiserfs_transaction_running(inode->i_sb)) {
  2491. struct reiserfs_transaction_handle *th;
  2492. th = (struct reiserfs_transaction_handle *)current->
  2493. journal_info;
  2494. BUG_ON(!th->t_refcount);
  2495. BUG_ON(!th->t_trans_id);
  2496. old_ref = th->t_refcount;
  2497. th->t_refcount++;
  2498. }
  2499. ret = __block_write_begin(page, pos, len, reiserfs_get_block);
  2500. if (ret && reiserfs_transaction_running(inode->i_sb)) {
  2501. struct reiserfs_transaction_handle *th = current->journal_info;
  2502. /*
  2503. * this gets a little ugly. If reiserfs_get_block returned an
  2504. * error and left a transacstion running, we've got to close
  2505. * it, and we've got to free handle if it was a persistent
  2506. * transaction.
  2507. *
  2508. * But, if we had nested into an existing transaction, we need
  2509. * to just drop the ref count on the handle.
  2510. *
  2511. * If old_ref == 0, the transaction is from reiserfs_get_block,
  2512. * and it was a persistent trans. Otherwise, it was nested
  2513. * above.
  2514. */
  2515. if (th->t_refcount > old_ref) {
  2516. if (old_ref)
  2517. th->t_refcount--;
  2518. else {
  2519. int err;
  2520. reiserfs_write_lock(inode->i_sb);
  2521. err = reiserfs_end_persistent_transaction(th);
  2522. reiserfs_write_unlock(inode->i_sb);
  2523. if (err)
  2524. ret = err;
  2525. }
  2526. }
  2527. }
  2528. if (ret) {
  2529. unlock_page(page);
  2530. put_page(page);
  2531. /* Truncate allocated blocks */
  2532. reiserfs_truncate_failed_write(inode);
  2533. }
  2534. return ret;
  2535. }
  2536. int __reiserfs_write_begin(struct page *page, unsigned from, unsigned len)
  2537. {
  2538. struct inode *inode = page->mapping->host;
  2539. int ret;
  2540. int old_ref = 0;
  2541. int depth;
  2542. depth = reiserfs_write_unlock_nested(inode->i_sb);
  2543. reiserfs_wait_on_write_block(inode->i_sb);
  2544. reiserfs_write_lock_nested(inode->i_sb, depth);
  2545. fix_tail_page_for_writing(page);
  2546. if (reiserfs_transaction_running(inode->i_sb)) {
  2547. struct reiserfs_transaction_handle *th;
  2548. th = (struct reiserfs_transaction_handle *)current->
  2549. journal_info;
  2550. BUG_ON(!th->t_refcount);
  2551. BUG_ON(!th->t_trans_id);
  2552. old_ref = th->t_refcount;
  2553. th->t_refcount++;
  2554. }
  2555. ret = __block_write_begin(page, from, len, reiserfs_get_block);
  2556. if (ret && reiserfs_transaction_running(inode->i_sb)) {
  2557. struct reiserfs_transaction_handle *th = current->journal_info;
  2558. /*
  2559. * this gets a little ugly. If reiserfs_get_block returned an
  2560. * error and left a transacstion running, we've got to close
  2561. * it, and we've got to free handle if it was a persistent
  2562. * transaction.
  2563. *
  2564. * But, if we had nested into an existing transaction, we need
  2565. * to just drop the ref count on the handle.
  2566. *
  2567. * If old_ref == 0, the transaction is from reiserfs_get_block,
  2568. * and it was a persistent trans. Otherwise, it was nested
  2569. * above.
  2570. */
  2571. if (th->t_refcount > old_ref) {
  2572. if (old_ref)
  2573. th->t_refcount--;
  2574. else {
  2575. int err;
  2576. reiserfs_write_lock(inode->i_sb);
  2577. err = reiserfs_end_persistent_transaction(th);
  2578. reiserfs_write_unlock(inode->i_sb);
  2579. if (err)
  2580. ret = err;
  2581. }
  2582. }
  2583. }
  2584. return ret;
  2585. }
  2586. static sector_t reiserfs_aop_bmap(struct address_space *as, sector_t block)
  2587. {
  2588. return generic_block_bmap(as, block, reiserfs_bmap);
  2589. }
  2590. static int reiserfs_write_end(struct file *file, struct address_space *mapping,
  2591. loff_t pos, unsigned len, unsigned copied,
  2592. struct page *page, void *fsdata)
  2593. {
  2594. struct inode *inode = page->mapping->host;
  2595. int ret = 0;
  2596. int update_sd = 0;
  2597. struct reiserfs_transaction_handle *th;
  2598. unsigned start;
  2599. bool locked = false;
  2600. reiserfs_wait_on_write_block(inode->i_sb);
  2601. if (reiserfs_transaction_running(inode->i_sb))
  2602. th = current->journal_info;
  2603. else
  2604. th = NULL;
  2605. start = pos & (PAGE_SIZE - 1);
  2606. if (unlikely(copied < len)) {
  2607. if (!PageUptodate(page))
  2608. copied = 0;
  2609. page_zero_new_buffers(page, start + copied, start + len);
  2610. }
  2611. flush_dcache_page(page);
  2612. reiserfs_commit_page(inode, page, start, start + copied);
  2613. /*
  2614. * generic_commit_write does this for us, but does not update the
  2615. * transaction tracking stuff when the size changes. So, we have
  2616. * to do the i_size updates here.
  2617. */
  2618. if (pos + copied > inode->i_size) {
  2619. struct reiserfs_transaction_handle myth;
  2620. reiserfs_write_lock(inode->i_sb);
  2621. locked = true;
  2622. /*
  2623. * If the file have grown beyond the border where it
  2624. * can have a tail, unmark it as needing a tail
  2625. * packing
  2626. */
  2627. if ((have_large_tails(inode->i_sb)
  2628. && inode->i_size > i_block_size(inode) * 4)
  2629. || (have_small_tails(inode->i_sb)
  2630. && inode->i_size > i_block_size(inode)))
  2631. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  2632. ret = journal_begin(&myth, inode->i_sb, 1);
  2633. if (ret)
  2634. goto journal_error;
  2635. reiserfs_update_inode_transaction(inode);
  2636. inode->i_size = pos + copied;
  2637. /*
  2638. * this will just nest into our transaction. It's important
  2639. * to use mark_inode_dirty so the inode gets pushed around on
  2640. * the dirty lists, and so that O_SYNC works as expected
  2641. */
  2642. mark_inode_dirty(inode);
  2643. reiserfs_update_sd(&myth, inode);
  2644. update_sd = 1;
  2645. ret = journal_end(&myth);
  2646. if (ret)
  2647. goto journal_error;
  2648. }
  2649. if (th) {
  2650. if (!locked) {
  2651. reiserfs_write_lock(inode->i_sb);
  2652. locked = true;
  2653. }
  2654. if (!update_sd)
  2655. mark_inode_dirty(inode);
  2656. ret = reiserfs_end_persistent_transaction(th);
  2657. if (ret)
  2658. goto out;
  2659. }
  2660. out:
  2661. if (locked)
  2662. reiserfs_write_unlock(inode->i_sb);
  2663. unlock_page(page);
  2664. put_page(page);
  2665. if (pos + len > inode->i_size)
  2666. reiserfs_truncate_failed_write(inode);
  2667. return ret == 0 ? copied : ret;
  2668. journal_error:
  2669. reiserfs_write_unlock(inode->i_sb);
  2670. locked = false;
  2671. if (th) {
  2672. if (!update_sd)
  2673. reiserfs_update_sd(th, inode);
  2674. ret = reiserfs_end_persistent_transaction(th);
  2675. }
  2676. goto out;
  2677. }
  2678. int reiserfs_commit_write(struct file *f, struct page *page,
  2679. unsigned from, unsigned to)
  2680. {
  2681. struct inode *inode = page->mapping->host;
  2682. loff_t pos = ((loff_t) page->index << PAGE_SHIFT) + to;
  2683. int ret = 0;
  2684. int update_sd = 0;
  2685. struct reiserfs_transaction_handle *th = NULL;
  2686. int depth;
  2687. depth = reiserfs_write_unlock_nested(inode->i_sb);
  2688. reiserfs_wait_on_write_block(inode->i_sb);
  2689. reiserfs_write_lock_nested(inode->i_sb, depth);
  2690. if (reiserfs_transaction_running(inode->i_sb)) {
  2691. th = current->journal_info;
  2692. }
  2693. reiserfs_commit_page(inode, page, from, to);
  2694. /*
  2695. * generic_commit_write does this for us, but does not update the
  2696. * transaction tracking stuff when the size changes. So, we have
  2697. * to do the i_size updates here.
  2698. */
  2699. if (pos > inode->i_size) {
  2700. struct reiserfs_transaction_handle myth;
  2701. /*
  2702. * If the file have grown beyond the border where it
  2703. * can have a tail, unmark it as needing a tail
  2704. * packing
  2705. */
  2706. if ((have_large_tails(inode->i_sb)
  2707. && inode->i_size > i_block_size(inode) * 4)
  2708. || (have_small_tails(inode->i_sb)
  2709. && inode->i_size > i_block_size(inode)))
  2710. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  2711. ret = journal_begin(&myth, inode->i_sb, 1);
  2712. if (ret)
  2713. goto journal_error;
  2714. reiserfs_update_inode_transaction(inode);
  2715. inode->i_size = pos;
  2716. /*
  2717. * this will just nest into our transaction. It's important
  2718. * to use mark_inode_dirty so the inode gets pushed around
  2719. * on the dirty lists, and so that O_SYNC works as expected
  2720. */
  2721. mark_inode_dirty(inode);
  2722. reiserfs_update_sd(&myth, inode);
  2723. update_sd = 1;
  2724. ret = journal_end(&myth);
  2725. if (ret)
  2726. goto journal_error;
  2727. }
  2728. if (th) {
  2729. if (!update_sd)
  2730. mark_inode_dirty(inode);
  2731. ret = reiserfs_end_persistent_transaction(th);
  2732. if (ret)
  2733. goto out;
  2734. }
  2735. out:
  2736. return ret;
  2737. journal_error:
  2738. if (th) {
  2739. if (!update_sd)
  2740. reiserfs_update_sd(th, inode);
  2741. ret = reiserfs_end_persistent_transaction(th);
  2742. }
  2743. return ret;
  2744. }
  2745. void sd_attrs_to_i_attrs(__u16 sd_attrs, struct inode *inode)
  2746. {
  2747. if (reiserfs_attrs(inode->i_sb)) {
  2748. if (sd_attrs & REISERFS_SYNC_FL)
  2749. inode->i_flags |= S_SYNC;
  2750. else
  2751. inode->i_flags &= ~S_SYNC;
  2752. if (sd_attrs & REISERFS_IMMUTABLE_FL)
  2753. inode->i_flags |= S_IMMUTABLE;
  2754. else
  2755. inode->i_flags &= ~S_IMMUTABLE;
  2756. if (sd_attrs & REISERFS_APPEND_FL)
  2757. inode->i_flags |= S_APPEND;
  2758. else
  2759. inode->i_flags &= ~S_APPEND;
  2760. if (sd_attrs & REISERFS_NOATIME_FL)
  2761. inode->i_flags |= S_NOATIME;
  2762. else
  2763. inode->i_flags &= ~S_NOATIME;
  2764. if (sd_attrs & REISERFS_NOTAIL_FL)
  2765. REISERFS_I(inode)->i_flags |= i_nopack_mask;
  2766. else
  2767. REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
  2768. }
  2769. }
  2770. /*
  2771. * decide if this buffer needs to stay around for data logging or ordered
  2772. * write purposes
  2773. */
  2774. static int invalidate_folio_can_drop(struct inode *inode, struct buffer_head *bh)
  2775. {
  2776. int ret = 1;
  2777. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  2778. lock_buffer(bh);
  2779. spin_lock(&j->j_dirty_buffers_lock);
  2780. if (!buffer_mapped(bh)) {
  2781. goto free_jh;
  2782. }
  2783. /*
  2784. * the page is locked, and the only places that log a data buffer
  2785. * also lock the page.
  2786. */
  2787. if (reiserfs_file_data_log(inode)) {
  2788. /*
  2789. * very conservative, leave the buffer pinned if
  2790. * anyone might need it.
  2791. */
  2792. if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
  2793. ret = 0;
  2794. }
  2795. } else if (buffer_dirty(bh)) {
  2796. struct reiserfs_journal_list *jl;
  2797. struct reiserfs_jh *jh = bh->b_private;
  2798. /*
  2799. * why is this safe?
  2800. * reiserfs_setattr updates i_size in the on disk
  2801. * stat data before allowing vmtruncate to be called.
  2802. *
  2803. * If buffer was put onto the ordered list for this
  2804. * transaction, we know for sure either this transaction
  2805. * or an older one already has updated i_size on disk,
  2806. * and this ordered data won't be referenced in the file
  2807. * if we crash.
  2808. *
  2809. * if the buffer was put onto the ordered list for an older
  2810. * transaction, we need to leave it around
  2811. */
  2812. if (jh && (jl = jh->jl)
  2813. && jl != SB_JOURNAL(inode->i_sb)->j_current_jl)
  2814. ret = 0;
  2815. }
  2816. free_jh:
  2817. if (ret && bh->b_private) {
  2818. reiserfs_free_jh(bh);
  2819. }
  2820. spin_unlock(&j->j_dirty_buffers_lock);
  2821. unlock_buffer(bh);
  2822. return ret;
  2823. }
  2824. /* clm -- taken from fs/buffer.c:block_invalidate_folio */
  2825. static void reiserfs_invalidate_folio(struct folio *folio, size_t offset,
  2826. size_t length)
  2827. {
  2828. struct buffer_head *head, *bh, *next;
  2829. struct inode *inode = folio->mapping->host;
  2830. unsigned int curr_off = 0;
  2831. unsigned int stop = offset + length;
  2832. int partial_page = (offset || length < folio_size(folio));
  2833. int ret = 1;
  2834. BUG_ON(!folio_test_locked(folio));
  2835. if (!partial_page)
  2836. folio_clear_checked(folio);
  2837. head = folio_buffers(folio);
  2838. if (!head)
  2839. goto out;
  2840. bh = head;
  2841. do {
  2842. unsigned int next_off = curr_off + bh->b_size;
  2843. next = bh->b_this_page;
  2844. if (next_off > stop)
  2845. goto out;
  2846. /*
  2847. * is this block fully invalidated?
  2848. */
  2849. if (offset <= curr_off) {
  2850. if (invalidate_folio_can_drop(inode, bh))
  2851. reiserfs_unmap_buffer(bh);
  2852. else
  2853. ret = 0;
  2854. }
  2855. curr_off = next_off;
  2856. bh = next;
  2857. } while (bh != head);
  2858. /*
  2859. * We release buffers only if the entire page is being invalidated.
  2860. * The get_block cached value has been unconditionally invalidated,
  2861. * so real IO is not possible anymore.
  2862. */
  2863. if (!partial_page && ret) {
  2864. ret = filemap_release_folio(folio, 0);
  2865. /* maybe should BUG_ON(!ret); - neilb */
  2866. }
  2867. out:
  2868. return;
  2869. }
  2870. static bool reiserfs_dirty_folio(struct address_space *mapping,
  2871. struct folio *folio)
  2872. {
  2873. if (reiserfs_file_data_log(mapping->host)) {
  2874. folio_set_checked(folio);
  2875. return filemap_dirty_folio(mapping, folio);
  2876. }
  2877. return block_dirty_folio(mapping, folio);
  2878. }
  2879. /*
  2880. * Returns true if the folio's buffers were dropped. The folio is locked.
  2881. *
  2882. * Takes j_dirty_buffers_lock to protect the b_assoc_buffers list_heads
  2883. * in the buffers at folio_buffers(folio).
  2884. *
  2885. * even in -o notail mode, we can't be sure an old mount without -o notail
  2886. * didn't create files with tails.
  2887. */
  2888. static bool reiserfs_release_folio(struct folio *folio, gfp_t unused_gfp_flags)
  2889. {
  2890. struct inode *inode = folio->mapping->host;
  2891. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  2892. struct buffer_head *head;
  2893. struct buffer_head *bh;
  2894. bool ret = true;
  2895. WARN_ON(folio_test_checked(folio));
  2896. spin_lock(&j->j_dirty_buffers_lock);
  2897. head = folio_buffers(folio);
  2898. bh = head;
  2899. do {
  2900. if (bh->b_private) {
  2901. if (!buffer_dirty(bh) && !buffer_locked(bh)) {
  2902. reiserfs_free_jh(bh);
  2903. } else {
  2904. ret = false;
  2905. break;
  2906. }
  2907. }
  2908. bh = bh->b_this_page;
  2909. } while (bh != head);
  2910. if (ret)
  2911. ret = try_to_free_buffers(folio);
  2912. spin_unlock(&j->j_dirty_buffers_lock);
  2913. return ret;
  2914. }
  2915. /*
  2916. * We thank Mingming Cao for helping us understand in great detail what
  2917. * to do in this section of the code.
  2918. */
  2919. static ssize_t reiserfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  2920. {
  2921. struct file *file = iocb->ki_filp;
  2922. struct inode *inode = file->f_mapping->host;
  2923. size_t count = iov_iter_count(iter);
  2924. ssize_t ret;
  2925. ret = blockdev_direct_IO(iocb, inode, iter,
  2926. reiserfs_get_blocks_direct_io);
  2927. /*
  2928. * In case of error extending write may have instantiated a few
  2929. * blocks outside i_size. Trim these off again.
  2930. */
  2931. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  2932. loff_t isize = i_size_read(inode);
  2933. loff_t end = iocb->ki_pos + count;
  2934. if ((end > isize) && inode_newsize_ok(inode, isize) == 0) {
  2935. truncate_setsize(inode, isize);
  2936. reiserfs_vfs_truncate_file(inode);
  2937. }
  2938. }
  2939. return ret;
  2940. }
  2941. int reiserfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
  2942. struct iattr *attr)
  2943. {
  2944. struct inode *inode = d_inode(dentry);
  2945. unsigned int ia_valid;
  2946. int error;
  2947. error = setattr_prepare(&init_user_ns, dentry, attr);
  2948. if (error)
  2949. return error;
  2950. /* must be turned off for recursive notify_change calls */
  2951. ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
  2952. if (is_quota_modification(mnt_userns, inode, attr)) {
  2953. error = dquot_initialize(inode);
  2954. if (error)
  2955. return error;
  2956. }
  2957. reiserfs_write_lock(inode->i_sb);
  2958. if (attr->ia_valid & ATTR_SIZE) {
  2959. /*
  2960. * version 2 items will be caught by the s_maxbytes check
  2961. * done for us in vmtruncate
  2962. */
  2963. if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5 &&
  2964. attr->ia_size > MAX_NON_LFS) {
  2965. reiserfs_write_unlock(inode->i_sb);
  2966. error = -EFBIG;
  2967. goto out;
  2968. }
  2969. inode_dio_wait(inode);
  2970. /* fill in hole pointers in the expanding truncate case. */
  2971. if (attr->ia_size > inode->i_size) {
  2972. loff_t pos = attr->ia_size;
  2973. if ((pos & (inode->i_sb->s_blocksize - 1)) == 0)
  2974. pos++;
  2975. error = generic_cont_expand_simple(inode, pos);
  2976. if (REISERFS_I(inode)->i_prealloc_count > 0) {
  2977. int err;
  2978. struct reiserfs_transaction_handle th;
  2979. /* we're changing at most 2 bitmaps, inode + super */
  2980. err = journal_begin(&th, inode->i_sb, 4);
  2981. if (!err) {
  2982. reiserfs_discard_prealloc(&th, inode);
  2983. err = journal_end(&th);
  2984. }
  2985. if (err)
  2986. error = err;
  2987. }
  2988. if (error) {
  2989. reiserfs_write_unlock(inode->i_sb);
  2990. goto out;
  2991. }
  2992. /*
  2993. * file size is changed, ctime and mtime are
  2994. * to be updated
  2995. */
  2996. attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
  2997. }
  2998. }
  2999. reiserfs_write_unlock(inode->i_sb);
  3000. if ((((attr->ia_valid & ATTR_UID) && (from_kuid(&init_user_ns, attr->ia_uid) & ~0xffff)) ||
  3001. ((attr->ia_valid & ATTR_GID) && (from_kgid(&init_user_ns, attr->ia_gid) & ~0xffff))) &&
  3002. (get_inode_sd_version(inode) == STAT_DATA_V1)) {
  3003. /* stat data of format v3.5 has 16 bit uid and gid */
  3004. error = -EINVAL;
  3005. goto out;
  3006. }
  3007. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  3008. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  3009. struct reiserfs_transaction_handle th;
  3010. int jbegin_count =
  3011. 2 *
  3012. (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
  3013. REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb)) +
  3014. 2;
  3015. error = reiserfs_chown_xattrs(inode, attr);
  3016. if (error)
  3017. return error;
  3018. /*
  3019. * (user+group)*(old+new) structure - we count quota
  3020. * info and , inode write (sb, inode)
  3021. */
  3022. reiserfs_write_lock(inode->i_sb);
  3023. error = journal_begin(&th, inode->i_sb, jbegin_count);
  3024. reiserfs_write_unlock(inode->i_sb);
  3025. if (error)
  3026. goto out;
  3027. error = dquot_transfer(mnt_userns, inode, attr);
  3028. reiserfs_write_lock(inode->i_sb);
  3029. if (error) {
  3030. journal_end(&th);
  3031. reiserfs_write_unlock(inode->i_sb);
  3032. goto out;
  3033. }
  3034. /*
  3035. * Update corresponding info in inode so that everything
  3036. * is in one transaction
  3037. */
  3038. if (attr->ia_valid & ATTR_UID)
  3039. inode->i_uid = attr->ia_uid;
  3040. if (attr->ia_valid & ATTR_GID)
  3041. inode->i_gid = attr->ia_gid;
  3042. mark_inode_dirty(inode);
  3043. error = journal_end(&th);
  3044. reiserfs_write_unlock(inode->i_sb);
  3045. if (error)
  3046. goto out;
  3047. }
  3048. if ((attr->ia_valid & ATTR_SIZE) &&
  3049. attr->ia_size != i_size_read(inode)) {
  3050. error = inode_newsize_ok(inode, attr->ia_size);
  3051. if (!error) {
  3052. /*
  3053. * Could race against reiserfs_file_release
  3054. * if called from NFS, so take tailpack mutex.
  3055. */
  3056. mutex_lock(&REISERFS_I(inode)->tailpack);
  3057. truncate_setsize(inode, attr->ia_size);
  3058. reiserfs_truncate_file(inode, 1);
  3059. mutex_unlock(&REISERFS_I(inode)->tailpack);
  3060. }
  3061. }
  3062. if (!error) {
  3063. setattr_copy(&init_user_ns, inode, attr);
  3064. mark_inode_dirty(inode);
  3065. }
  3066. if (!error && reiserfs_posixacl(inode->i_sb)) {
  3067. if (attr->ia_valid & ATTR_MODE)
  3068. error = reiserfs_acl_chmod(inode);
  3069. }
  3070. out:
  3071. return error;
  3072. }
  3073. const struct address_space_operations reiserfs_address_space_operations = {
  3074. .writepage = reiserfs_writepage,
  3075. .read_folio = reiserfs_read_folio,
  3076. .readahead = reiserfs_readahead,
  3077. .release_folio = reiserfs_release_folio,
  3078. .invalidate_folio = reiserfs_invalidate_folio,
  3079. .write_begin = reiserfs_write_begin,
  3080. .write_end = reiserfs_write_end,
  3081. .bmap = reiserfs_aop_bmap,
  3082. .direct_IO = reiserfs_direct_IO,
  3083. .dirty_folio = reiserfs_dirty_folio,
  3084. };