inode.c 92 KB

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