inode.c 91 KB

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