inode.c 87 KB

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