inode.c 87 KB

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