inode.c 93 KB

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