inode.c 92 KB

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