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