inode.c 91 KB

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