inode.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  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 = kzalloc(min(blocks_needed, max_to_insert) * UNFM_P_SIZE, GFP_NOFS);
  941. if (!un) {
  942. un = &unf_single;
  943. blocks_needed = 1;
  944. max_to_insert = 0;
  945. }
  946. }
  947. if (blocks_needed <= max_to_insert) {
  948. /*
  949. * we are going to add target block to
  950. * the file. Use allocated block for that
  951. */
  952. un[blocks_needed - 1] =
  953. cpu_to_le32(allocated_block_nr);
  954. set_block_dev_mapped(bh_result,
  955. allocated_block_nr, inode);
  956. set_buffer_new(bh_result);
  957. done = 1;
  958. } else {
  959. /* paste hole to the indirect item */
  960. /*
  961. * If kmalloc failed, max_to_insert becomes
  962. * zero and it means we only have space for
  963. * one block
  964. */
  965. blocks_needed =
  966. max_to_insert ? max_to_insert : 1;
  967. }
  968. retval =
  969. reiserfs_paste_into_item(th, &path, &tmp_key, inode,
  970. (char *)un,
  971. UNFM_P_SIZE *
  972. blocks_needed);
  973. if (blocks_needed != 1)
  974. kfree(un);
  975. if (retval) {
  976. reiserfs_free_block(th, inode,
  977. allocated_block_nr, 1);
  978. goto failure;
  979. }
  980. if (!done) {
  981. /*
  982. * We need to mark new file size in case
  983. * this function will be interrupted/aborted
  984. * later on. And we may do this only for
  985. * holes.
  986. */
  987. inode->i_size +=
  988. inode->i_sb->s_blocksize * blocks_needed;
  989. }
  990. }
  991. if (done == 1)
  992. break;
  993. /*
  994. * this loop could log more blocks than we had originally
  995. * asked for. So, we have to allow the transaction to end
  996. * if it is too big or too full. Update the inode so things
  997. * are consistent if we crash before the function returns
  998. * release the path so that anybody waiting on the path before
  999. * ending their transaction will be able to continue.
  1000. */
  1001. if (journal_transaction_should_end(th, th->t_blocks_allocated)) {
  1002. retval = restart_transaction(th, inode, &path);
  1003. if (retval)
  1004. goto failure;
  1005. }
  1006. /*
  1007. * inserting indirect pointers for a hole can take a
  1008. * long time. reschedule if needed and also release the write
  1009. * lock for others.
  1010. */
  1011. reiserfs_cond_resched(inode->i_sb);
  1012. retval = search_for_position_by_key(inode->i_sb, &key, &path);
  1013. if (retval == IO_ERROR) {
  1014. retval = -EIO;
  1015. goto failure;
  1016. }
  1017. if (retval == POSITION_FOUND) {
  1018. reiserfs_warning(inode->i_sb, "vs-825",
  1019. "%K should not be found", &key);
  1020. retval = -EEXIST;
  1021. if (allocated_block_nr)
  1022. reiserfs_free_block(th, inode,
  1023. allocated_block_nr, 1);
  1024. pathrelse(&path);
  1025. goto failure;
  1026. }
  1027. bh = get_last_bh(&path);
  1028. ih = tp_item_head(&path);
  1029. item = tp_item_body(&path);
  1030. pos_in_item = path.pos_in_item;
  1031. } while (1);
  1032. retval = 0;
  1033. failure:
  1034. if (th && (!dangle || (retval && !th->t_trans_id))) {
  1035. int err;
  1036. if (th->t_trans_id)
  1037. reiserfs_update_sd(th, inode);
  1038. err = reiserfs_end_persistent_transaction(th);
  1039. if (err)
  1040. retval = err;
  1041. }
  1042. reiserfs_write_unlock(inode->i_sb);
  1043. reiserfs_check_path(&path);
  1044. return retval;
  1045. }
  1046. static int
  1047. reiserfs_readpages(struct file *file, struct address_space *mapping,
  1048. struct list_head *pages, unsigned nr_pages)
  1049. {
  1050. return mpage_readpages(mapping, pages, nr_pages, reiserfs_get_block);
  1051. }
  1052. /*
  1053. * Compute real number of used bytes by file
  1054. * Following three functions can go away when we'll have enough space in
  1055. * stat item
  1056. */
  1057. static int real_space_diff(struct inode *inode, int sd_size)
  1058. {
  1059. int bytes;
  1060. loff_t blocksize = inode->i_sb->s_blocksize;
  1061. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode))
  1062. return sd_size;
  1063. /*
  1064. * End of file is also in full block with indirect reference, so round
  1065. * up to the next block.
  1066. *
  1067. * there is just no way to know if the tail is actually packed
  1068. * on the file, so we have to assume it isn't. When we pack the
  1069. * tail, we add 4 bytes to pretend there really is an unformatted
  1070. * node pointer
  1071. */
  1072. bytes =
  1073. ((inode->i_size +
  1074. (blocksize - 1)) >> inode->i_sb->s_blocksize_bits) * UNFM_P_SIZE +
  1075. sd_size;
  1076. return bytes;
  1077. }
  1078. static inline loff_t to_real_used_space(struct inode *inode, ulong blocks,
  1079. int sd_size)
  1080. {
  1081. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
  1082. return inode->i_size +
  1083. (loff_t) (real_space_diff(inode, sd_size));
  1084. }
  1085. return ((loff_t) real_space_diff(inode, sd_size)) +
  1086. (((loff_t) blocks) << 9);
  1087. }
  1088. /* Compute number of blocks used by file in ReiserFS counting */
  1089. static inline ulong to_fake_used_blocks(struct inode *inode, int sd_size)
  1090. {
  1091. loff_t bytes = inode_get_bytes(inode);
  1092. loff_t real_space = real_space_diff(inode, sd_size);
  1093. /* keeps fsck and non-quota versions of reiserfs happy */
  1094. if (S_ISLNK(inode->i_mode) || S_ISDIR(inode->i_mode)) {
  1095. bytes += (loff_t) 511;
  1096. }
  1097. /*
  1098. * files from before the quota patch might i_blocks such that
  1099. * bytes < real_space. Deal with that here to prevent it from
  1100. * going negative.
  1101. */
  1102. if (bytes < real_space)
  1103. return 0;
  1104. return (bytes - real_space) >> 9;
  1105. }
  1106. /*
  1107. * BAD: new directories have stat data of new type and all other items
  1108. * of old type. Version stored in the inode says about body items, so
  1109. * in update_stat_data we can not rely on inode, but have to check
  1110. * item version directly
  1111. */
  1112. /* called by read_locked_inode */
  1113. static void init_inode(struct inode *inode, struct treepath *path)
  1114. {
  1115. struct buffer_head *bh;
  1116. struct item_head *ih;
  1117. __u32 rdev;
  1118. bh = PATH_PLAST_BUFFER(path);
  1119. ih = tp_item_head(path);
  1120. copy_key(INODE_PKEY(inode), &ih->ih_key);
  1121. INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
  1122. REISERFS_I(inode)->i_flags = 0;
  1123. REISERFS_I(inode)->i_prealloc_block = 0;
  1124. REISERFS_I(inode)->i_prealloc_count = 0;
  1125. REISERFS_I(inode)->i_trans_id = 0;
  1126. REISERFS_I(inode)->i_jl = NULL;
  1127. reiserfs_init_xattr_rwsem(inode);
  1128. if (stat_data_v1(ih)) {
  1129. struct stat_data_v1 *sd =
  1130. (struct stat_data_v1 *)ih_item_body(bh, ih);
  1131. unsigned long blocks;
  1132. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1133. set_inode_sd_version(inode, STAT_DATA_V1);
  1134. inode->i_mode = sd_v1_mode(sd);
  1135. set_nlink(inode, sd_v1_nlink(sd));
  1136. i_uid_write(inode, sd_v1_uid(sd));
  1137. i_gid_write(inode, sd_v1_gid(sd));
  1138. inode->i_size = sd_v1_size(sd);
  1139. inode->i_atime.tv_sec = sd_v1_atime(sd);
  1140. inode->i_mtime.tv_sec = sd_v1_mtime(sd);
  1141. inode->i_ctime.tv_sec = sd_v1_ctime(sd);
  1142. inode->i_atime.tv_nsec = 0;
  1143. inode->i_ctime.tv_nsec = 0;
  1144. inode->i_mtime.tv_nsec = 0;
  1145. inode->i_blocks = sd_v1_blocks(sd);
  1146. inode->i_generation = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1147. blocks = (inode->i_size + 511) >> 9;
  1148. blocks = _ROUND_UP(blocks, inode->i_sb->s_blocksize >> 9);
  1149. /*
  1150. * there was a bug in <=3.5.23 when i_blocks could take
  1151. * negative values. Starting from 3.5.17 this value could
  1152. * even be stored in stat data. For such files we set
  1153. * i_blocks based on file size. Just 2 notes: this can be
  1154. * wrong for sparse files. On-disk value will be only
  1155. * updated if file's inode will ever change
  1156. */
  1157. if (inode->i_blocks > blocks) {
  1158. inode->i_blocks = blocks;
  1159. }
  1160. rdev = sd_v1_rdev(sd);
  1161. REISERFS_I(inode)->i_first_direct_byte =
  1162. sd_v1_first_direct_byte(sd);
  1163. /*
  1164. * an early bug in the quota code can give us an odd
  1165. * number for the block count. This is incorrect, fix it here.
  1166. */
  1167. if (inode->i_blocks & 1) {
  1168. inode->i_blocks++;
  1169. }
  1170. inode_set_bytes(inode,
  1171. to_real_used_space(inode, inode->i_blocks,
  1172. SD_V1_SIZE));
  1173. /*
  1174. * nopack is initially zero for v1 objects. For v2 objects,
  1175. * nopack is initialised from sd_attrs
  1176. */
  1177. REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
  1178. } else {
  1179. /*
  1180. * new stat data found, but object may have old items
  1181. * (directories and symlinks)
  1182. */
  1183. struct stat_data *sd = (struct stat_data *)ih_item_body(bh, ih);
  1184. inode->i_mode = sd_v2_mode(sd);
  1185. set_nlink(inode, sd_v2_nlink(sd));
  1186. i_uid_write(inode, sd_v2_uid(sd));
  1187. inode->i_size = sd_v2_size(sd);
  1188. i_gid_write(inode, sd_v2_gid(sd));
  1189. inode->i_mtime.tv_sec = sd_v2_mtime(sd);
  1190. inode->i_atime.tv_sec = sd_v2_atime(sd);
  1191. inode->i_ctime.tv_sec = sd_v2_ctime(sd);
  1192. inode->i_ctime.tv_nsec = 0;
  1193. inode->i_mtime.tv_nsec = 0;
  1194. inode->i_atime.tv_nsec = 0;
  1195. inode->i_blocks = sd_v2_blocks(sd);
  1196. rdev = sd_v2_rdev(sd);
  1197. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1198. inode->i_generation =
  1199. le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1200. else
  1201. inode->i_generation = sd_v2_generation(sd);
  1202. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1203. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1204. else
  1205. set_inode_item_key_version(inode, KEY_FORMAT_3_6);
  1206. REISERFS_I(inode)->i_first_direct_byte = 0;
  1207. set_inode_sd_version(inode, STAT_DATA_V2);
  1208. inode_set_bytes(inode,
  1209. to_real_used_space(inode, inode->i_blocks,
  1210. SD_V2_SIZE));
  1211. /*
  1212. * read persistent inode attributes from sd and initialise
  1213. * generic inode flags from them
  1214. */
  1215. REISERFS_I(inode)->i_attrs = sd_v2_attrs(sd);
  1216. sd_attrs_to_i_attrs(sd_v2_attrs(sd), inode);
  1217. }
  1218. pathrelse(path);
  1219. if (S_ISREG(inode->i_mode)) {
  1220. inode->i_op = &reiserfs_file_inode_operations;
  1221. inode->i_fop = &reiserfs_file_operations;
  1222. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  1223. } else if (S_ISDIR(inode->i_mode)) {
  1224. inode->i_op = &reiserfs_dir_inode_operations;
  1225. inode->i_fop = &reiserfs_dir_operations;
  1226. } else if (S_ISLNK(inode->i_mode)) {
  1227. inode->i_op = &reiserfs_symlink_inode_operations;
  1228. inode_nohighmem(inode);
  1229. inode->i_mapping->a_ops = &reiserfs_address_space_operations;
  1230. } else {
  1231. inode->i_blocks = 0;
  1232. inode->i_op = &reiserfs_special_inode_operations;
  1233. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  1234. }
  1235. }
  1236. /* update new stat data with inode fields */
  1237. static void inode2sd(void *sd, struct inode *inode, loff_t size)
  1238. {
  1239. struct stat_data *sd_v2 = (struct stat_data *)sd;
  1240. set_sd_v2_mode(sd_v2, inode->i_mode);
  1241. set_sd_v2_nlink(sd_v2, inode->i_nlink);
  1242. set_sd_v2_uid(sd_v2, i_uid_read(inode));
  1243. set_sd_v2_size(sd_v2, size);
  1244. set_sd_v2_gid(sd_v2, i_gid_read(inode));
  1245. set_sd_v2_mtime(sd_v2, inode->i_mtime.tv_sec);
  1246. set_sd_v2_atime(sd_v2, inode->i_atime.tv_sec);
  1247. set_sd_v2_ctime(sd_v2, inode->i_ctime.tv_sec);
  1248. set_sd_v2_blocks(sd_v2, to_fake_used_blocks(inode, SD_V2_SIZE));
  1249. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1250. set_sd_v2_rdev(sd_v2, new_encode_dev(inode->i_rdev));
  1251. else
  1252. set_sd_v2_generation(sd_v2, inode->i_generation);
  1253. set_sd_v2_attrs(sd_v2, REISERFS_I(inode)->i_attrs);
  1254. }
  1255. /* used to copy inode's fields to old stat data */
  1256. static void inode2sd_v1(void *sd, struct inode *inode, loff_t size)
  1257. {
  1258. struct stat_data_v1 *sd_v1 = (struct stat_data_v1 *)sd;
  1259. set_sd_v1_mode(sd_v1, inode->i_mode);
  1260. set_sd_v1_uid(sd_v1, i_uid_read(inode));
  1261. set_sd_v1_gid(sd_v1, i_gid_read(inode));
  1262. set_sd_v1_nlink(sd_v1, inode->i_nlink);
  1263. set_sd_v1_size(sd_v1, size);
  1264. set_sd_v1_atime(sd_v1, inode->i_atime.tv_sec);
  1265. set_sd_v1_ctime(sd_v1, inode->i_ctime.tv_sec);
  1266. set_sd_v1_mtime(sd_v1, inode->i_mtime.tv_sec);
  1267. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  1268. set_sd_v1_rdev(sd_v1, new_encode_dev(inode->i_rdev));
  1269. else
  1270. set_sd_v1_blocks(sd_v1, to_fake_used_blocks(inode, SD_V1_SIZE));
  1271. /* Sigh. i_first_direct_byte is back */
  1272. set_sd_v1_first_direct_byte(sd_v1,
  1273. REISERFS_I(inode)->i_first_direct_byte);
  1274. }
  1275. /*
  1276. * NOTE, you must prepare the buffer head before sending it here,
  1277. * and then log it after the call
  1278. */
  1279. static void update_stat_data(struct treepath *path, struct inode *inode,
  1280. loff_t size)
  1281. {
  1282. struct buffer_head *bh;
  1283. struct item_head *ih;
  1284. bh = PATH_PLAST_BUFFER(path);
  1285. ih = tp_item_head(path);
  1286. if (!is_statdata_le_ih(ih))
  1287. reiserfs_panic(inode->i_sb, "vs-13065", "key %k, found item %h",
  1288. INODE_PKEY(inode), ih);
  1289. /* path points to old stat data */
  1290. if (stat_data_v1(ih)) {
  1291. inode2sd_v1(ih_item_body(bh, ih), inode, size);
  1292. } else {
  1293. inode2sd(ih_item_body(bh, ih), inode, size);
  1294. }
  1295. return;
  1296. }
  1297. void reiserfs_update_sd_size(struct reiserfs_transaction_handle *th,
  1298. struct inode *inode, loff_t size)
  1299. {
  1300. struct cpu_key key;
  1301. INITIALIZE_PATH(path);
  1302. struct buffer_head *bh;
  1303. int fs_gen;
  1304. struct item_head *ih, tmp_ih;
  1305. int retval;
  1306. BUG_ON(!th->t_trans_id);
  1307. /* key type is unimportant */
  1308. make_cpu_key(&key, inode, SD_OFFSET, TYPE_STAT_DATA, 3);
  1309. for (;;) {
  1310. int pos;
  1311. /* look for the object's stat data */
  1312. retval = search_item(inode->i_sb, &key, &path);
  1313. if (retval == IO_ERROR) {
  1314. reiserfs_error(inode->i_sb, "vs-13050",
  1315. "i/o failure occurred trying to "
  1316. "update %K stat data", &key);
  1317. return;
  1318. }
  1319. if (retval == ITEM_NOT_FOUND) {
  1320. pos = PATH_LAST_POSITION(&path);
  1321. pathrelse(&path);
  1322. if (inode->i_nlink == 0) {
  1323. /*reiserfs_warning (inode->i_sb, "vs-13050: reiserfs_update_sd: i_nlink == 0, stat data not found"); */
  1324. return;
  1325. }
  1326. reiserfs_warning(inode->i_sb, "vs-13060",
  1327. "stat data of object %k (nlink == %d) "
  1328. "not found (pos %d)",
  1329. INODE_PKEY(inode), inode->i_nlink,
  1330. pos);
  1331. reiserfs_check_path(&path);
  1332. return;
  1333. }
  1334. /*
  1335. * sigh, prepare_for_journal might schedule. When it
  1336. * schedules the FS might change. We have to detect that,
  1337. * and loop back to the search if the stat data item has moved
  1338. */
  1339. bh = get_last_bh(&path);
  1340. ih = tp_item_head(&path);
  1341. copy_item_head(&tmp_ih, ih);
  1342. fs_gen = get_generation(inode->i_sb);
  1343. reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
  1344. /* Stat_data item has been moved after scheduling. */
  1345. if (fs_changed(fs_gen, inode->i_sb)
  1346. && item_moved(&tmp_ih, &path)) {
  1347. reiserfs_restore_prepared_buffer(inode->i_sb, bh);
  1348. continue;
  1349. }
  1350. break;
  1351. }
  1352. update_stat_data(&path, inode, size);
  1353. journal_mark_dirty(th, bh);
  1354. pathrelse(&path);
  1355. return;
  1356. }
  1357. /*
  1358. * reiserfs_read_locked_inode is called to read the inode off disk, and it
  1359. * does a make_bad_inode when things go wrong. But, we need to make sure
  1360. * and clear the key in the private portion of the inode, otherwise a
  1361. * corresponding iput might try to delete whatever object the inode last
  1362. * represented.
  1363. */
  1364. static void reiserfs_make_bad_inode(struct inode *inode)
  1365. {
  1366. memset(INODE_PKEY(inode), 0, KEY_SIZE);
  1367. make_bad_inode(inode);
  1368. }
  1369. /*
  1370. * initially this function was derived from minix or ext2's analog and
  1371. * evolved as the prototype did
  1372. */
  1373. int reiserfs_init_locked_inode(struct inode *inode, void *p)
  1374. {
  1375. struct reiserfs_iget_args *args = (struct reiserfs_iget_args *)p;
  1376. inode->i_ino = args->objectid;
  1377. INODE_PKEY(inode)->k_dir_id = cpu_to_le32(args->dirid);
  1378. return 0;
  1379. }
  1380. /*
  1381. * looks for stat data in the tree, and fills up the fields of in-core
  1382. * inode stat data fields
  1383. */
  1384. void reiserfs_read_locked_inode(struct inode *inode,
  1385. struct reiserfs_iget_args *args)
  1386. {
  1387. INITIALIZE_PATH(path_to_sd);
  1388. struct cpu_key key;
  1389. unsigned long dirino;
  1390. int retval;
  1391. dirino = args->dirid;
  1392. /*
  1393. * set version 1, version 2 could be used too, because stat data
  1394. * key is the same in both versions
  1395. */
  1396. key.version = KEY_FORMAT_3_5;
  1397. key.on_disk_key.k_dir_id = dirino;
  1398. key.on_disk_key.k_objectid = inode->i_ino;
  1399. key.on_disk_key.k_offset = 0;
  1400. key.on_disk_key.k_type = 0;
  1401. /* look for the object's stat data */
  1402. retval = search_item(inode->i_sb, &key, &path_to_sd);
  1403. if (retval == IO_ERROR) {
  1404. reiserfs_error(inode->i_sb, "vs-13070",
  1405. "i/o failure occurred trying to find "
  1406. "stat data of %K", &key);
  1407. reiserfs_make_bad_inode(inode);
  1408. return;
  1409. }
  1410. /* a stale NFS handle can trigger this without it being an error */
  1411. if (retval != ITEM_FOUND) {
  1412. pathrelse(&path_to_sd);
  1413. reiserfs_make_bad_inode(inode);
  1414. clear_nlink(inode);
  1415. return;
  1416. }
  1417. init_inode(inode, &path_to_sd);
  1418. /*
  1419. * It is possible that knfsd is trying to access inode of a file
  1420. * that is being removed from the disk by some other thread. As we
  1421. * update sd on unlink all that is required is to check for nlink
  1422. * here. This bug was first found by Sizif when debugging
  1423. * SquidNG/Butterfly, forgotten, and found again after Philippe
  1424. * Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
  1425. * More logical fix would require changes in fs/inode.c:iput() to
  1426. * remove inode from hash-table _after_ fs cleaned disk stuff up and
  1427. * in iget() to return NULL if I_FREEING inode is found in
  1428. * hash-table.
  1429. */
  1430. /*
  1431. * Currently there is one place where it's ok to meet inode with
  1432. * nlink==0: processing of open-unlinked and half-truncated files
  1433. * during mount (fs/reiserfs/super.c:finish_unfinished()).
  1434. */
  1435. if ((inode->i_nlink == 0) &&
  1436. !REISERFS_SB(inode->i_sb)->s_is_unlinked_ok) {
  1437. reiserfs_warning(inode->i_sb, "vs-13075",
  1438. "dead inode read from disk %K. "
  1439. "This is likely to be race with knfsd. Ignore",
  1440. &key);
  1441. reiserfs_make_bad_inode(inode);
  1442. }
  1443. /* init inode should be relsing */
  1444. reiserfs_check_path(&path_to_sd);
  1445. /*
  1446. * Stat data v1 doesn't support ACLs.
  1447. */
  1448. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1449. cache_no_acl(inode);
  1450. }
  1451. /*
  1452. * reiserfs_find_actor() - "find actor" reiserfs supplies to iget5_locked().
  1453. *
  1454. * @inode: inode from hash table to check
  1455. * @opaque: "cookie" passed to iget5_locked(). This is &reiserfs_iget_args.
  1456. *
  1457. * This function is called by iget5_locked() to distinguish reiserfs inodes
  1458. * having the same inode numbers. Such inodes can only exist due to some
  1459. * error condition. One of them should be bad. Inodes with identical
  1460. * inode numbers (objectids) are distinguished by parent directory ids.
  1461. *
  1462. */
  1463. int reiserfs_find_actor(struct inode *inode, void *opaque)
  1464. {
  1465. struct reiserfs_iget_args *args;
  1466. args = opaque;
  1467. /* args is already in CPU order */
  1468. return (inode->i_ino == args->objectid) &&
  1469. (le32_to_cpu(INODE_PKEY(inode)->k_dir_id) == args->dirid);
  1470. }
  1471. struct inode *reiserfs_iget(struct super_block *s, const struct cpu_key *key)
  1472. {
  1473. struct inode *inode;
  1474. struct reiserfs_iget_args args;
  1475. int depth;
  1476. args.objectid = key->on_disk_key.k_objectid;
  1477. args.dirid = key->on_disk_key.k_dir_id;
  1478. depth = reiserfs_write_unlock_nested(s);
  1479. inode = iget5_locked(s, key->on_disk_key.k_objectid,
  1480. reiserfs_find_actor, reiserfs_init_locked_inode,
  1481. (void *)(&args));
  1482. reiserfs_write_lock_nested(s, depth);
  1483. if (!inode)
  1484. return ERR_PTR(-ENOMEM);
  1485. if (inode->i_state & I_NEW) {
  1486. reiserfs_read_locked_inode(inode, &args);
  1487. unlock_new_inode(inode);
  1488. }
  1489. if (comp_short_keys(INODE_PKEY(inode), key) || is_bad_inode(inode)) {
  1490. /* either due to i/o error or a stale NFS handle */
  1491. iput(inode);
  1492. inode = NULL;
  1493. }
  1494. return inode;
  1495. }
  1496. static struct dentry *reiserfs_get_dentry(struct super_block *sb,
  1497. u32 objectid, u32 dir_id, u32 generation)
  1498. {
  1499. struct cpu_key key;
  1500. struct inode *inode;
  1501. key.on_disk_key.k_objectid = objectid;
  1502. key.on_disk_key.k_dir_id = dir_id;
  1503. reiserfs_write_lock(sb);
  1504. inode = reiserfs_iget(sb, &key);
  1505. if (inode && !IS_ERR(inode) && generation != 0 &&
  1506. generation != inode->i_generation) {
  1507. iput(inode);
  1508. inode = NULL;
  1509. }
  1510. reiserfs_write_unlock(sb);
  1511. return d_obtain_alias(inode);
  1512. }
  1513. struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1514. int fh_len, int fh_type)
  1515. {
  1516. /*
  1517. * fhtype happens to reflect the number of u32s encoded.
  1518. * due to a bug in earlier code, fhtype might indicate there
  1519. * are more u32s then actually fitted.
  1520. * so if fhtype seems to be more than len, reduce fhtype.
  1521. * Valid types are:
  1522. * 2 - objectid + dir_id - legacy support
  1523. * 3 - objectid + dir_id + generation
  1524. * 4 - objectid + dir_id + objectid and dirid of parent - legacy
  1525. * 5 - objectid + dir_id + generation + objectid and dirid of parent
  1526. * 6 - as above plus generation of directory
  1527. * 6 does not fit in NFSv2 handles
  1528. */
  1529. if (fh_type > fh_len) {
  1530. if (fh_type != 6 || fh_len != 5)
  1531. reiserfs_warning(sb, "reiserfs-13077",
  1532. "nfsd/reiserfs, fhtype=%d, len=%d - odd",
  1533. fh_type, fh_len);
  1534. fh_type = fh_len;
  1535. }
  1536. if (fh_len < 2)
  1537. return NULL;
  1538. return reiserfs_get_dentry(sb, fid->raw[0], fid->raw[1],
  1539. (fh_type == 3 || fh_type >= 5) ? fid->raw[2] : 0);
  1540. }
  1541. struct dentry *reiserfs_fh_to_parent(struct super_block *sb, struct fid *fid,
  1542. int fh_len, int fh_type)
  1543. {
  1544. if (fh_type > fh_len)
  1545. fh_type = fh_len;
  1546. if (fh_type < 4)
  1547. return NULL;
  1548. return reiserfs_get_dentry(sb,
  1549. (fh_type >= 5) ? fid->raw[3] : fid->raw[2],
  1550. (fh_type >= 5) ? fid->raw[4] : fid->raw[3],
  1551. (fh_type == 6) ? fid->raw[5] : 0);
  1552. }
  1553. int reiserfs_encode_fh(struct inode *inode, __u32 * data, int *lenp,
  1554. struct inode *parent)
  1555. {
  1556. int maxlen = *lenp;
  1557. if (parent && (maxlen < 5)) {
  1558. *lenp = 5;
  1559. return FILEID_INVALID;
  1560. } else if (maxlen < 3) {
  1561. *lenp = 3;
  1562. return FILEID_INVALID;
  1563. }
  1564. data[0] = inode->i_ino;
  1565. data[1] = le32_to_cpu(INODE_PKEY(inode)->k_dir_id);
  1566. data[2] = inode->i_generation;
  1567. *lenp = 3;
  1568. if (parent) {
  1569. data[3] = parent->i_ino;
  1570. data[4] = le32_to_cpu(INODE_PKEY(parent)->k_dir_id);
  1571. *lenp = 5;
  1572. if (maxlen >= 6) {
  1573. data[5] = parent->i_generation;
  1574. *lenp = 6;
  1575. }
  1576. }
  1577. return *lenp;
  1578. }
  1579. /*
  1580. * looks for stat data, then copies fields to it, marks the buffer
  1581. * containing stat data as dirty
  1582. */
  1583. /*
  1584. * reiserfs inodes are never really dirty, since the dirty inode call
  1585. * always logs them. This call allows the VFS inode marking routines
  1586. * to properly mark inodes for datasync and such, but only actually
  1587. * does something when called for a synchronous update.
  1588. */
  1589. int reiserfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1590. {
  1591. struct reiserfs_transaction_handle th;
  1592. int jbegin_count = 1;
  1593. if (sb_rdonly(inode->i_sb))
  1594. return -EROFS;
  1595. /*
  1596. * memory pressure can sometimes initiate write_inode calls with
  1597. * sync == 1,
  1598. * these cases are just when the system needs ram, not when the
  1599. * inode needs to reach disk for safety, and they can safely be
  1600. * ignored because the altered inode has already been logged.
  1601. */
  1602. if (wbc->sync_mode == WB_SYNC_ALL && !(current->flags & PF_MEMALLOC)) {
  1603. reiserfs_write_lock(inode->i_sb);
  1604. if (!journal_begin(&th, inode->i_sb, jbegin_count)) {
  1605. reiserfs_update_sd(&th, inode);
  1606. journal_end_sync(&th);
  1607. }
  1608. reiserfs_write_unlock(inode->i_sb);
  1609. }
  1610. return 0;
  1611. }
  1612. /*
  1613. * stat data of new object is inserted already, this inserts the item
  1614. * containing "." and ".." entries
  1615. */
  1616. static int reiserfs_new_directory(struct reiserfs_transaction_handle *th,
  1617. struct inode *inode,
  1618. struct item_head *ih, struct treepath *path,
  1619. struct inode *dir)
  1620. {
  1621. struct super_block *sb = th->t_super;
  1622. char empty_dir[EMPTY_DIR_SIZE];
  1623. char *body = empty_dir;
  1624. struct cpu_key key;
  1625. int retval;
  1626. BUG_ON(!th->t_trans_id);
  1627. _make_cpu_key(&key, KEY_FORMAT_3_5, le32_to_cpu(ih->ih_key.k_dir_id),
  1628. le32_to_cpu(ih->ih_key.k_objectid), DOT_OFFSET,
  1629. TYPE_DIRENTRY, 3 /*key length */ );
  1630. /*
  1631. * compose item head for new item. Directories consist of items of
  1632. * old type (ITEM_VERSION_1). Do not set key (second arg is 0), it
  1633. * is done by reiserfs_new_inode
  1634. */
  1635. if (old_format_only(sb)) {
  1636. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
  1637. TYPE_DIRENTRY, EMPTY_DIR_SIZE_V1, 2);
  1638. make_empty_dir_item_v1(body, ih->ih_key.k_dir_id,
  1639. ih->ih_key.k_objectid,
  1640. INODE_PKEY(dir)->k_dir_id,
  1641. INODE_PKEY(dir)->k_objectid);
  1642. } else {
  1643. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, DOT_OFFSET,
  1644. TYPE_DIRENTRY, EMPTY_DIR_SIZE, 2);
  1645. make_empty_dir_item(body, ih->ih_key.k_dir_id,
  1646. ih->ih_key.k_objectid,
  1647. INODE_PKEY(dir)->k_dir_id,
  1648. INODE_PKEY(dir)->k_objectid);
  1649. }
  1650. /* look for place in the tree for new item */
  1651. retval = search_item(sb, &key, path);
  1652. if (retval == IO_ERROR) {
  1653. reiserfs_error(sb, "vs-13080",
  1654. "i/o failure occurred creating new directory");
  1655. return -EIO;
  1656. }
  1657. if (retval == ITEM_FOUND) {
  1658. pathrelse(path);
  1659. reiserfs_warning(sb, "vs-13070",
  1660. "object with this key exists (%k)",
  1661. &(ih->ih_key));
  1662. return -EEXIST;
  1663. }
  1664. /* insert item, that is empty directory item */
  1665. return reiserfs_insert_item(th, path, &key, ih, inode, body);
  1666. }
  1667. /*
  1668. * stat data of object has been inserted, this inserts the item
  1669. * containing the body of symlink
  1670. */
  1671. static int reiserfs_new_symlink(struct reiserfs_transaction_handle *th,
  1672. struct inode *inode,
  1673. struct item_head *ih,
  1674. struct treepath *path, const char *symname,
  1675. int item_len)
  1676. {
  1677. struct super_block *sb = th->t_super;
  1678. struct cpu_key key;
  1679. int retval;
  1680. BUG_ON(!th->t_trans_id);
  1681. _make_cpu_key(&key, KEY_FORMAT_3_5,
  1682. le32_to_cpu(ih->ih_key.k_dir_id),
  1683. le32_to_cpu(ih->ih_key.k_objectid),
  1684. 1, TYPE_DIRECT, 3 /*key length */ );
  1685. make_le_item_head(ih, NULL, KEY_FORMAT_3_5, 1, TYPE_DIRECT, item_len,
  1686. 0 /*free_space */ );
  1687. /* look for place in the tree for new item */
  1688. retval = search_item(sb, &key, path);
  1689. if (retval == IO_ERROR) {
  1690. reiserfs_error(sb, "vs-13080",
  1691. "i/o failure occurred creating new symlink");
  1692. return -EIO;
  1693. }
  1694. if (retval == ITEM_FOUND) {
  1695. pathrelse(path);
  1696. reiserfs_warning(sb, "vs-13080",
  1697. "object with this key exists (%k)",
  1698. &(ih->ih_key));
  1699. return -EEXIST;
  1700. }
  1701. /* insert item, that is body of symlink */
  1702. return reiserfs_insert_item(th, path, &key, ih, inode, symname);
  1703. }
  1704. /*
  1705. * inserts the stat data into the tree, and then calls
  1706. * reiserfs_new_directory (to insert ".", ".." item if new object is
  1707. * directory) or reiserfs_new_symlink (to insert symlink body if new
  1708. * object is symlink) or nothing (if new object is regular file)
  1709. * NOTE! uid and gid must already be set in the inode. If we return
  1710. * non-zero due to an error, we have to drop the quota previously allocated
  1711. * for the fresh inode. This can only be done outside a transaction, so
  1712. * if we return non-zero, we also end the transaction.
  1713. *
  1714. * @th: active transaction handle
  1715. * @dir: parent directory for new inode
  1716. * @mode: mode of new inode
  1717. * @symname: symlink contents if inode is symlink
  1718. * @isize: 0 for regular file, EMPTY_DIR_SIZE for dirs, strlen(symname) for
  1719. * symlinks
  1720. * @inode: inode to be filled
  1721. * @security: optional security context to associate with this inode
  1722. */
  1723. int reiserfs_new_inode(struct reiserfs_transaction_handle *th,
  1724. struct inode *dir, umode_t mode, const char *symname,
  1725. /* 0 for regular, EMTRY_DIR_SIZE for dirs,
  1726. strlen (symname) for symlinks) */
  1727. loff_t i_size, struct dentry *dentry,
  1728. struct inode *inode,
  1729. struct reiserfs_security_handle *security)
  1730. {
  1731. struct super_block *sb = dir->i_sb;
  1732. struct reiserfs_iget_args args;
  1733. INITIALIZE_PATH(path_to_key);
  1734. struct cpu_key key;
  1735. struct item_head ih;
  1736. struct stat_data sd;
  1737. int retval;
  1738. int err;
  1739. int depth;
  1740. BUG_ON(!th->t_trans_id);
  1741. depth = reiserfs_write_unlock_nested(sb);
  1742. err = dquot_alloc_inode(inode);
  1743. reiserfs_write_lock_nested(sb, depth);
  1744. if (err)
  1745. goto out_end_trans;
  1746. if (!dir->i_nlink) {
  1747. err = -EPERM;
  1748. goto out_bad_inode;
  1749. }
  1750. /* item head of new item */
  1751. ih.ih_key.k_dir_id = reiserfs_choose_packing(dir);
  1752. ih.ih_key.k_objectid = cpu_to_le32(reiserfs_get_unused_objectid(th));
  1753. if (!ih.ih_key.k_objectid) {
  1754. err = -ENOMEM;
  1755. goto out_bad_inode;
  1756. }
  1757. args.objectid = inode->i_ino = le32_to_cpu(ih.ih_key.k_objectid);
  1758. if (old_format_only(sb))
  1759. make_le_item_head(&ih, NULL, KEY_FORMAT_3_5, SD_OFFSET,
  1760. TYPE_STAT_DATA, SD_V1_SIZE, MAX_US_INT);
  1761. else
  1762. make_le_item_head(&ih, NULL, KEY_FORMAT_3_6, SD_OFFSET,
  1763. TYPE_STAT_DATA, SD_SIZE, MAX_US_INT);
  1764. memcpy(INODE_PKEY(inode), &ih.ih_key, KEY_SIZE);
  1765. args.dirid = le32_to_cpu(ih.ih_key.k_dir_id);
  1766. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1767. err = insert_inode_locked4(inode, args.objectid,
  1768. reiserfs_find_actor, &args);
  1769. reiserfs_write_lock_nested(inode->i_sb, depth);
  1770. if (err) {
  1771. err = -EINVAL;
  1772. goto out_bad_inode;
  1773. }
  1774. if (old_format_only(sb))
  1775. /*
  1776. * not a perfect generation count, as object ids can be reused,
  1777. * but this is as good as reiserfs can do right now.
  1778. * note that the private part of inode isn't filled in yet,
  1779. * we have to use the directory.
  1780. */
  1781. inode->i_generation = le32_to_cpu(INODE_PKEY(dir)->k_objectid);
  1782. else
  1783. #if defined( USE_INODE_GENERATION_COUNTER )
  1784. inode->i_generation =
  1785. le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
  1786. #else
  1787. inode->i_generation = ++event;
  1788. #endif
  1789. /* fill stat data */
  1790. set_nlink(inode, (S_ISDIR(mode) ? 2 : 1));
  1791. /* uid and gid must already be set by the caller for quota init */
  1792. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  1793. inode->i_size = i_size;
  1794. inode->i_blocks = 0;
  1795. inode->i_bytes = 0;
  1796. REISERFS_I(inode)->i_first_direct_byte = S_ISLNK(mode) ? 1 :
  1797. U32_MAX /*NO_BYTES_IN_DIRECT_ITEM */ ;
  1798. INIT_LIST_HEAD(&REISERFS_I(inode)->i_prealloc_list);
  1799. REISERFS_I(inode)->i_flags = 0;
  1800. REISERFS_I(inode)->i_prealloc_block = 0;
  1801. REISERFS_I(inode)->i_prealloc_count = 0;
  1802. REISERFS_I(inode)->i_trans_id = 0;
  1803. REISERFS_I(inode)->i_jl = NULL;
  1804. REISERFS_I(inode)->i_attrs =
  1805. REISERFS_I(dir)->i_attrs & REISERFS_INHERIT_MASK;
  1806. sd_attrs_to_i_attrs(REISERFS_I(inode)->i_attrs, inode);
  1807. reiserfs_init_xattr_rwsem(inode);
  1808. /* key to search for correct place for new stat data */
  1809. _make_cpu_key(&key, KEY_FORMAT_3_6, le32_to_cpu(ih.ih_key.k_dir_id),
  1810. le32_to_cpu(ih.ih_key.k_objectid), SD_OFFSET,
  1811. TYPE_STAT_DATA, 3 /*key length */ );
  1812. /* find proper place for inserting of stat data */
  1813. retval = search_item(sb, &key, &path_to_key);
  1814. if (retval == IO_ERROR) {
  1815. err = -EIO;
  1816. goto out_bad_inode;
  1817. }
  1818. if (retval == ITEM_FOUND) {
  1819. pathrelse(&path_to_key);
  1820. err = -EEXIST;
  1821. goto out_bad_inode;
  1822. }
  1823. if (old_format_only(sb)) {
  1824. /* i_uid or i_gid is too big to be stored in stat data v3.5 */
  1825. if (i_uid_read(inode) & ~0xffff || i_gid_read(inode) & ~0xffff) {
  1826. pathrelse(&path_to_key);
  1827. err = -EINVAL;
  1828. goto out_bad_inode;
  1829. }
  1830. inode2sd_v1(&sd, inode, inode->i_size);
  1831. } else {
  1832. inode2sd(&sd, inode, inode->i_size);
  1833. }
  1834. /*
  1835. * store in in-core inode the key of stat data and version all
  1836. * object items will have (directory items will have old offset
  1837. * format, other new objects will consist of new items)
  1838. */
  1839. if (old_format_only(sb) || S_ISDIR(mode) || S_ISLNK(mode))
  1840. set_inode_item_key_version(inode, KEY_FORMAT_3_5);
  1841. else
  1842. set_inode_item_key_version(inode, KEY_FORMAT_3_6);
  1843. if (old_format_only(sb))
  1844. set_inode_sd_version(inode, STAT_DATA_V1);
  1845. else
  1846. set_inode_sd_version(inode, STAT_DATA_V2);
  1847. /* insert the stat data into the tree */
  1848. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1849. if (REISERFS_I(dir)->new_packing_locality)
  1850. th->displace_new_blocks = 1;
  1851. #endif
  1852. retval =
  1853. reiserfs_insert_item(th, &path_to_key, &key, &ih, inode,
  1854. (char *)(&sd));
  1855. if (retval) {
  1856. err = retval;
  1857. reiserfs_check_path(&path_to_key);
  1858. goto out_bad_inode;
  1859. }
  1860. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  1861. if (!th->displace_new_blocks)
  1862. REISERFS_I(dir)->new_packing_locality = 0;
  1863. #endif
  1864. if (S_ISDIR(mode)) {
  1865. /* insert item with "." and ".." */
  1866. retval =
  1867. reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
  1868. }
  1869. if (S_ISLNK(mode)) {
  1870. /* insert body of symlink */
  1871. if (!old_format_only(sb))
  1872. i_size = ROUND_UP(i_size);
  1873. retval =
  1874. reiserfs_new_symlink(th, inode, &ih, &path_to_key, symname,
  1875. i_size);
  1876. }
  1877. if (retval) {
  1878. err = retval;
  1879. reiserfs_check_path(&path_to_key);
  1880. journal_end(th);
  1881. goto out_inserted_sd;
  1882. }
  1883. if (reiserfs_posixacl(inode->i_sb)) {
  1884. reiserfs_write_unlock(inode->i_sb);
  1885. retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
  1886. reiserfs_write_lock(inode->i_sb);
  1887. if (retval) {
  1888. err = retval;
  1889. reiserfs_check_path(&path_to_key);
  1890. journal_end(th);
  1891. goto out_inserted_sd;
  1892. }
  1893. } else if (inode->i_sb->s_flags & SB_POSIXACL) {
  1894. reiserfs_warning(inode->i_sb, "jdm-13090",
  1895. "ACLs aren't enabled in the fs, "
  1896. "but vfs thinks they are!");
  1897. } else if (IS_PRIVATE(dir))
  1898. inode->i_flags |= S_PRIVATE;
  1899. if (security->name) {
  1900. reiserfs_write_unlock(inode->i_sb);
  1901. retval = reiserfs_security_write(th, inode, security);
  1902. reiserfs_write_lock(inode->i_sb);
  1903. if (retval) {
  1904. err = retval;
  1905. reiserfs_check_path(&path_to_key);
  1906. retval = journal_end(th);
  1907. if (retval)
  1908. err = retval;
  1909. goto out_inserted_sd;
  1910. }
  1911. }
  1912. reiserfs_update_sd(th, inode);
  1913. reiserfs_check_path(&path_to_key);
  1914. return 0;
  1915. out_bad_inode:
  1916. /* Invalidate the object, nothing was inserted yet */
  1917. INODE_PKEY(inode)->k_objectid = 0;
  1918. /* Quota change must be inside a transaction for journaling */
  1919. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1920. dquot_free_inode(inode);
  1921. reiserfs_write_lock_nested(inode->i_sb, depth);
  1922. out_end_trans:
  1923. journal_end(th);
  1924. /*
  1925. * Drop can be outside and it needs more credits so it's better
  1926. * to have it outside
  1927. */
  1928. depth = reiserfs_write_unlock_nested(inode->i_sb);
  1929. dquot_drop(inode);
  1930. reiserfs_write_lock_nested(inode->i_sb, depth);
  1931. inode->i_flags |= S_NOQUOTA;
  1932. make_bad_inode(inode);
  1933. out_inserted_sd:
  1934. clear_nlink(inode);
  1935. th->t_trans_id = 0; /* so the caller can't use this handle later */
  1936. unlock_new_inode(inode); /* OK to do even if we hadn't locked it */
  1937. iput(inode);
  1938. return err;
  1939. }
  1940. /*
  1941. * finds the tail page in the page cache,
  1942. * reads the last block in.
  1943. *
  1944. * On success, page_result is set to a locked, pinned page, and bh_result
  1945. * is set to an up to date buffer for the last block in the file. returns 0.
  1946. *
  1947. * tail conversion is not done, so bh_result might not be valid for writing
  1948. * check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
  1949. * trying to write the block.
  1950. *
  1951. * on failure, nonzero is returned, page_result and bh_result are untouched.
  1952. */
  1953. static int grab_tail_page(struct inode *inode,
  1954. struct page **page_result,
  1955. struct buffer_head **bh_result)
  1956. {
  1957. /*
  1958. * we want the page with the last byte in the file,
  1959. * not the page that will hold the next byte for appending
  1960. */
  1961. unsigned long index = (inode->i_size - 1) >> PAGE_SHIFT;
  1962. unsigned long pos = 0;
  1963. unsigned long start = 0;
  1964. unsigned long blocksize = inode->i_sb->s_blocksize;
  1965. unsigned long offset = (inode->i_size) & (PAGE_SIZE - 1);
  1966. struct buffer_head *bh;
  1967. struct buffer_head *head;
  1968. struct page *page;
  1969. int error;
  1970. /*
  1971. * we know that we are only called with inode->i_size > 0.
  1972. * we also know that a file tail can never be as big as a block
  1973. * If i_size % blocksize == 0, our file is currently block aligned
  1974. * and it won't need converting or zeroing after a truncate.
  1975. */
  1976. if ((offset & (blocksize - 1)) == 0) {
  1977. return -ENOENT;
  1978. }
  1979. page = grab_cache_page(inode->i_mapping, index);
  1980. error = -ENOMEM;
  1981. if (!page) {
  1982. goto out;
  1983. }
  1984. /* start within the page of the last block in the file */
  1985. start = (offset / blocksize) * blocksize;
  1986. error = __block_write_begin(page, start, offset - start,
  1987. reiserfs_get_block_create_0);
  1988. if (error)
  1989. goto unlock;
  1990. head = page_buffers(page);
  1991. bh = head;
  1992. do {
  1993. if (pos >= start) {
  1994. break;
  1995. }
  1996. bh = bh->b_this_page;
  1997. pos += blocksize;
  1998. } while (bh != head);
  1999. if (!buffer_uptodate(bh)) {
  2000. /*
  2001. * note, this should never happen, prepare_write should be
  2002. * taking care of this for us. If the buffer isn't up to
  2003. * date, I've screwed up the code to find the buffer, or the
  2004. * code to call prepare_write
  2005. */
  2006. reiserfs_error(inode->i_sb, "clm-6000",
  2007. "error reading block %lu", bh->b_blocknr);
  2008. error = -EIO;
  2009. goto unlock;
  2010. }
  2011. *bh_result = bh;
  2012. *page_result = page;
  2013. out:
  2014. return error;
  2015. unlock:
  2016. unlock_page(page);
  2017. put_page(page);
  2018. return error;
  2019. }
  2020. /*
  2021. * vfs version of truncate file. Must NOT be called with
  2022. * a transaction already started.
  2023. *
  2024. * some code taken from block_truncate_page
  2025. */
  2026. int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
  2027. {
  2028. struct reiserfs_transaction_handle th;
  2029. /* we want the offset for the first byte after the end of the file */
  2030. unsigned long offset = inode->i_size & (PAGE_SIZE - 1);
  2031. unsigned blocksize = inode->i_sb->s_blocksize;
  2032. unsigned length;
  2033. struct page *page = NULL;
  2034. int error;
  2035. struct buffer_head *bh = NULL;
  2036. int err2;
  2037. reiserfs_write_lock(inode->i_sb);
  2038. if (inode->i_size > 0) {
  2039. error = grab_tail_page(inode, &page, &bh);
  2040. if (error) {
  2041. /*
  2042. * -ENOENT means we truncated past the end of the
  2043. * file, and get_block_create_0 could not find a
  2044. * block to read in, which is ok.
  2045. */
  2046. if (error != -ENOENT)
  2047. reiserfs_error(inode->i_sb, "clm-6001",
  2048. "grab_tail_page failed %d",
  2049. error);
  2050. page = NULL;
  2051. bh = NULL;
  2052. }
  2053. }
  2054. /*
  2055. * so, if page != NULL, we have a buffer head for the offset at
  2056. * the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
  2057. * then we have an unformatted node. Otherwise, we have a direct item,
  2058. * and no zeroing is required on disk. We zero after the truncate,
  2059. * because the truncate might pack the item anyway
  2060. * (it will unmap bh if it packs).
  2061. *
  2062. * it is enough to reserve space in transaction for 2 balancings:
  2063. * one for "save" link adding and another for the first
  2064. * cut_from_item. 1 is for update_sd
  2065. */
  2066. error = journal_begin(&th, inode->i_sb,
  2067. JOURNAL_PER_BALANCE_CNT * 2 + 1);
  2068. if (error)
  2069. goto out;
  2070. reiserfs_update_inode_transaction(inode);
  2071. if (update_timestamps)
  2072. /*
  2073. * we are doing real truncate: if the system crashes
  2074. * before the last transaction of truncating gets committed
  2075. * - on reboot the file either appears truncated properly
  2076. * or not truncated at all
  2077. */
  2078. add_save_link(&th, inode, 1);
  2079. err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
  2080. error = journal_end(&th);
  2081. if (error)
  2082. goto out;
  2083. /* check reiserfs_do_truncate after ending the transaction */
  2084. if (err2) {
  2085. error = err2;
  2086. goto out;
  2087. }
  2088. if (update_timestamps) {
  2089. error = remove_save_link(inode, 1 /* truncate */);
  2090. if (error)
  2091. goto out;
  2092. }
  2093. if (page) {
  2094. length = offset & (blocksize - 1);
  2095. /* if we are not on a block boundary */
  2096. if (length) {
  2097. length = blocksize - length;
  2098. zero_user(page, offset, length);
  2099. if (buffer_mapped(bh) && bh->b_blocknr != 0) {
  2100. mark_buffer_dirty(bh);
  2101. }
  2102. }
  2103. unlock_page(page);
  2104. put_page(page);
  2105. }
  2106. reiserfs_write_unlock(inode->i_sb);
  2107. return 0;
  2108. out:
  2109. if (page) {
  2110. unlock_page(page);
  2111. put_page(page);
  2112. }
  2113. reiserfs_write_unlock(inode->i_sb);
  2114. return error;
  2115. }
  2116. static int map_block_for_writepage(struct inode *inode,
  2117. struct buffer_head *bh_result,
  2118. unsigned long block)
  2119. {
  2120. struct reiserfs_transaction_handle th;
  2121. int fs_gen;
  2122. struct item_head tmp_ih;
  2123. struct item_head *ih;
  2124. struct buffer_head *bh;
  2125. __le32 *item;
  2126. struct cpu_key key;
  2127. INITIALIZE_PATH(path);
  2128. int pos_in_item;
  2129. int jbegin_count = JOURNAL_PER_BALANCE_CNT;
  2130. loff_t byte_offset = ((loff_t)block << inode->i_sb->s_blocksize_bits)+1;
  2131. int retval;
  2132. int use_get_block = 0;
  2133. int bytes_copied = 0;
  2134. int copy_size;
  2135. int trans_running = 0;
  2136. /*
  2137. * catch places below that try to log something without
  2138. * starting a trans
  2139. */
  2140. th.t_trans_id = 0;
  2141. if (!buffer_uptodate(bh_result)) {
  2142. return -EIO;
  2143. }
  2144. kmap(bh_result->b_page);
  2145. start_over:
  2146. reiserfs_write_lock(inode->i_sb);
  2147. make_cpu_key(&key, inode, byte_offset, TYPE_ANY, 3);
  2148. research:
  2149. retval = search_for_position_by_key(inode->i_sb, &key, &path);
  2150. if (retval != POSITION_FOUND) {
  2151. use_get_block = 1;
  2152. goto out;
  2153. }
  2154. bh = get_last_bh(&path);
  2155. ih = tp_item_head(&path);
  2156. item = tp_item_body(&path);
  2157. pos_in_item = path.pos_in_item;
  2158. /* we've found an unformatted node */
  2159. if (indirect_item_found(retval, ih)) {
  2160. if (bytes_copied > 0) {
  2161. reiserfs_warning(inode->i_sb, "clm-6002",
  2162. "bytes_copied %d", bytes_copied);
  2163. }
  2164. if (!get_block_num(item, pos_in_item)) {
  2165. /* crap, we are writing to a hole */
  2166. use_get_block = 1;
  2167. goto out;
  2168. }
  2169. set_block_dev_mapped(bh_result,
  2170. get_block_num(item, pos_in_item), inode);
  2171. } else if (is_direct_le_ih(ih)) {
  2172. char *p;
  2173. p = page_address(bh_result->b_page);
  2174. p += (byte_offset - 1) & (PAGE_SIZE - 1);
  2175. copy_size = ih_item_len(ih) - pos_in_item;
  2176. fs_gen = get_generation(inode->i_sb);
  2177. copy_item_head(&tmp_ih, ih);
  2178. if (!trans_running) {
  2179. /* vs-3050 is gone, no need to drop the path */
  2180. retval = journal_begin(&th, inode->i_sb, jbegin_count);
  2181. if (retval)
  2182. goto out;
  2183. reiserfs_update_inode_transaction(inode);
  2184. trans_running = 1;
  2185. if (fs_changed(fs_gen, inode->i_sb)
  2186. && item_moved(&tmp_ih, &path)) {
  2187. reiserfs_restore_prepared_buffer(inode->i_sb,
  2188. bh);
  2189. goto research;
  2190. }
  2191. }
  2192. reiserfs_prepare_for_journal(inode->i_sb, bh, 1);
  2193. if (fs_changed(fs_gen, inode->i_sb)
  2194. && item_moved(&tmp_ih, &path)) {
  2195. reiserfs_restore_prepared_buffer(inode->i_sb, bh);
  2196. goto research;
  2197. }
  2198. memcpy(ih_item_body(bh, ih) + pos_in_item, p + bytes_copied,
  2199. copy_size);
  2200. journal_mark_dirty(&th, bh);
  2201. bytes_copied += copy_size;
  2202. set_block_dev_mapped(bh_result, 0, inode);
  2203. /* are there still bytes left? */
  2204. if (bytes_copied < bh_result->b_size &&
  2205. (byte_offset + bytes_copied) < inode->i_size) {
  2206. set_cpu_key_k_offset(&key,
  2207. cpu_key_k_offset(&key) +
  2208. copy_size);
  2209. goto research;
  2210. }
  2211. } else {
  2212. reiserfs_warning(inode->i_sb, "clm-6003",
  2213. "bad item inode %lu", inode->i_ino);
  2214. retval = -EIO;
  2215. goto out;
  2216. }
  2217. retval = 0;
  2218. out:
  2219. pathrelse(&path);
  2220. if (trans_running) {
  2221. int err = journal_end(&th);
  2222. if (err)
  2223. retval = err;
  2224. trans_running = 0;
  2225. }
  2226. reiserfs_write_unlock(inode->i_sb);
  2227. /* this is where we fill in holes in the file. */
  2228. if (use_get_block) {
  2229. retval = reiserfs_get_block(inode, block, bh_result,
  2230. GET_BLOCK_CREATE | GET_BLOCK_NO_IMUX
  2231. | GET_BLOCK_NO_DANGLE);
  2232. if (!retval) {
  2233. if (!buffer_mapped(bh_result)
  2234. || bh_result->b_blocknr == 0) {
  2235. /* get_block failed to find a mapped unformatted node. */
  2236. use_get_block = 0;
  2237. goto start_over;
  2238. }
  2239. }
  2240. }
  2241. kunmap(bh_result->b_page);
  2242. if (!retval && buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
  2243. /*
  2244. * we've copied data from the page into the direct item, so the
  2245. * buffer in the page is now clean, mark it to reflect that.
  2246. */
  2247. lock_buffer(bh_result);
  2248. clear_buffer_dirty(bh_result);
  2249. unlock_buffer(bh_result);
  2250. }
  2251. return retval;
  2252. }
  2253. /*
  2254. * mason@suse.com: updated in 2.5.54 to follow the same general io
  2255. * start/recovery path as __block_write_full_page, along with special
  2256. * code to handle reiserfs tails.
  2257. */
  2258. static int reiserfs_write_full_page(struct page *page,
  2259. struct writeback_control *wbc)
  2260. {
  2261. struct inode *inode = page->mapping->host;
  2262. unsigned long end_index = inode->i_size >> PAGE_SHIFT;
  2263. int error = 0;
  2264. unsigned long block;
  2265. sector_t last_block;
  2266. struct buffer_head *head, *bh;
  2267. int partial = 0;
  2268. int nr = 0;
  2269. int checked = PageChecked(page);
  2270. struct reiserfs_transaction_handle th;
  2271. struct super_block *s = inode->i_sb;
  2272. int bh_per_page = PAGE_SIZE / s->s_blocksize;
  2273. th.t_trans_id = 0;
  2274. /* no logging allowed when nonblocking or from PF_MEMALLOC */
  2275. if (checked && (current->flags & PF_MEMALLOC)) {
  2276. redirty_page_for_writepage(wbc, page);
  2277. unlock_page(page);
  2278. return 0;
  2279. }
  2280. /*
  2281. * The page dirty bit is cleared before writepage is called, which
  2282. * means we have to tell create_empty_buffers to make dirty buffers
  2283. * The page really should be up to date at this point, so tossing
  2284. * in the BH_Uptodate is just a sanity check.
  2285. */
  2286. if (!page_has_buffers(page)) {
  2287. create_empty_buffers(page, s->s_blocksize,
  2288. (1 << BH_Dirty) | (1 << BH_Uptodate));
  2289. }
  2290. head = page_buffers(page);
  2291. /*
  2292. * last page in the file, zero out any contents past the
  2293. * last byte in the file
  2294. */
  2295. if (page->index >= end_index) {
  2296. unsigned last_offset;
  2297. last_offset = inode->i_size & (PAGE_SIZE - 1);
  2298. /* no file contents in this page */
  2299. if (page->index >= end_index + 1 || !last_offset) {
  2300. unlock_page(page);
  2301. return 0;
  2302. }
  2303. zero_user_segment(page, last_offset, PAGE_SIZE);
  2304. }
  2305. bh = head;
  2306. block = page->index << (PAGE_SHIFT - s->s_blocksize_bits);
  2307. last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
  2308. /* first map all the buffers, logging any direct items we find */
  2309. do {
  2310. if (block > last_block) {
  2311. /*
  2312. * This can happen when the block size is less than
  2313. * the page size. The corresponding bytes in the page
  2314. * were zero filled above
  2315. */
  2316. clear_buffer_dirty(bh);
  2317. set_buffer_uptodate(bh);
  2318. } else if ((checked || buffer_dirty(bh)) &&
  2319. (!buffer_mapped(bh) || (buffer_mapped(bh)
  2320. && bh->b_blocknr ==
  2321. 0))) {
  2322. /*
  2323. * not mapped yet, or it points to a direct item, search
  2324. * the btree for the mapping info, and log any direct
  2325. * items found
  2326. */
  2327. if ((error = map_block_for_writepage(inode, bh, block))) {
  2328. goto fail;
  2329. }
  2330. }
  2331. bh = bh->b_this_page;
  2332. block++;
  2333. } while (bh != head);
  2334. /*
  2335. * we start the transaction after map_block_for_writepage,
  2336. * because it can create holes in the file (an unbounded operation).
  2337. * starting it here, we can make a reliable estimate for how many
  2338. * blocks we're going to log
  2339. */
  2340. if (checked) {
  2341. ClearPageChecked(page);
  2342. reiserfs_write_lock(s);
  2343. error = journal_begin(&th, s, bh_per_page + 1);
  2344. if (error) {
  2345. reiserfs_write_unlock(s);
  2346. goto fail;
  2347. }
  2348. reiserfs_update_inode_transaction(inode);
  2349. }
  2350. /* now go through and lock any dirty buffers on the page */
  2351. do {
  2352. get_bh(bh);
  2353. if (!buffer_mapped(bh))
  2354. continue;
  2355. if (buffer_mapped(bh) && bh->b_blocknr == 0)
  2356. continue;
  2357. if (checked) {
  2358. reiserfs_prepare_for_journal(s, bh, 1);
  2359. journal_mark_dirty(&th, bh);
  2360. continue;
  2361. }
  2362. /*
  2363. * from this point on, we know the buffer is mapped to a
  2364. * real block and not a direct item
  2365. */
  2366. if (wbc->sync_mode != WB_SYNC_NONE) {
  2367. lock_buffer(bh);
  2368. } else {
  2369. if (!trylock_buffer(bh)) {
  2370. redirty_page_for_writepage(wbc, page);
  2371. continue;
  2372. }
  2373. }
  2374. if (test_clear_buffer_dirty(bh)) {
  2375. mark_buffer_async_write(bh);
  2376. } else {
  2377. unlock_buffer(bh);
  2378. }
  2379. } while ((bh = bh->b_this_page) != head);
  2380. if (checked) {
  2381. error = journal_end(&th);
  2382. reiserfs_write_unlock(s);
  2383. if (error)
  2384. goto fail;
  2385. }
  2386. BUG_ON(PageWriteback(page));
  2387. set_page_writeback(page);
  2388. unlock_page(page);
  2389. /*
  2390. * since any buffer might be the only dirty buffer on the page,
  2391. * the first submit_bh can bring the page out of writeback.
  2392. * be careful with the buffers.
  2393. */
  2394. do {
  2395. struct buffer_head *next = bh->b_this_page;
  2396. if (buffer_async_write(bh)) {
  2397. submit_bh(REQ_OP_WRITE, 0, bh);
  2398. nr++;
  2399. }
  2400. put_bh(bh);
  2401. bh = next;
  2402. } while (bh != head);
  2403. error = 0;
  2404. done:
  2405. if (nr == 0) {
  2406. /*
  2407. * if this page only had a direct item, it is very possible for
  2408. * no io to be required without there being an error. Or,
  2409. * someone else could have locked them and sent them down the
  2410. * pipe without locking the page
  2411. */
  2412. bh = head;
  2413. do {
  2414. if (!buffer_uptodate(bh)) {
  2415. partial = 1;
  2416. break;
  2417. }
  2418. bh = bh->b_this_page;
  2419. } while (bh != head);
  2420. if (!partial)
  2421. SetPageUptodate(page);
  2422. end_page_writeback(page);
  2423. }
  2424. return error;
  2425. fail:
  2426. /*
  2427. * catches various errors, we need to make sure any valid dirty blocks
  2428. * get to the media. The page is currently locked and not marked for
  2429. * writeback
  2430. */
  2431. ClearPageUptodate(page);
  2432. bh = head;
  2433. do {
  2434. get_bh(bh);
  2435. if (buffer_mapped(bh) && buffer_dirty(bh) && bh->b_blocknr) {
  2436. lock_buffer(bh);
  2437. mark_buffer_async_write(bh);
  2438. } else {
  2439. /*
  2440. * clear any dirty bits that might have come from
  2441. * getting attached to a dirty page
  2442. */
  2443. clear_buffer_dirty(bh);
  2444. }
  2445. bh = bh->b_this_page;
  2446. } while (bh != head);
  2447. SetPageError(page);
  2448. BUG_ON(PageWriteback(page));
  2449. set_page_writeback(page);
  2450. unlock_page(page);
  2451. do {
  2452. struct buffer_head *next = bh->b_this_page;
  2453. if (buffer_async_write(bh)) {
  2454. clear_buffer_dirty(bh);
  2455. submit_bh(REQ_OP_WRITE, 0, bh);
  2456. nr++;
  2457. }
  2458. put_bh(bh);
  2459. bh = next;
  2460. } while (bh != head);
  2461. goto done;
  2462. }
  2463. static int reiserfs_readpage(struct file *f, struct page *page)
  2464. {
  2465. return block_read_full_page(page, reiserfs_get_block);
  2466. }
  2467. static int reiserfs_writepage(struct page *page, struct writeback_control *wbc)
  2468. {
  2469. struct inode *inode = page->mapping->host;
  2470. reiserfs_wait_on_write_block(inode->i_sb);
  2471. return reiserfs_write_full_page(page, wbc);
  2472. }
  2473. static void reiserfs_truncate_failed_write(struct inode *inode)
  2474. {
  2475. truncate_inode_pages(inode->i_mapping, inode->i_size);
  2476. reiserfs_truncate_file(inode, 0);
  2477. }
  2478. static int reiserfs_write_begin(struct file *file,
  2479. struct address_space *mapping,
  2480. loff_t pos, unsigned len, unsigned flags,
  2481. struct page **pagep, void **fsdata)
  2482. {
  2483. struct inode *inode;
  2484. struct page *page;
  2485. pgoff_t index;
  2486. int ret;
  2487. int old_ref = 0;
  2488. inode = mapping->host;
  2489. *fsdata = NULL;
  2490. if (flags & AOP_FLAG_CONT_EXPAND &&
  2491. (pos & (inode->i_sb->s_blocksize - 1)) == 0) {
  2492. pos ++;
  2493. *fsdata = (void *)(unsigned long)flags;
  2494. }
  2495. index = pos >> PAGE_SHIFT;
  2496. page = grab_cache_page_write_begin(mapping, index, flags);
  2497. if (!page)
  2498. return -ENOMEM;
  2499. *pagep = page;
  2500. reiserfs_wait_on_write_block(inode->i_sb);
  2501. fix_tail_page_for_writing(page);
  2502. if (reiserfs_transaction_running(inode->i_sb)) {
  2503. struct reiserfs_transaction_handle *th;
  2504. th = (struct reiserfs_transaction_handle *)current->
  2505. journal_info;
  2506. BUG_ON(!th->t_refcount);
  2507. BUG_ON(!th->t_trans_id);
  2508. old_ref = th->t_refcount;
  2509. th->t_refcount++;
  2510. }
  2511. ret = __block_write_begin(page, pos, len, reiserfs_get_block);
  2512. if (ret && reiserfs_transaction_running(inode->i_sb)) {
  2513. struct reiserfs_transaction_handle *th = current->journal_info;
  2514. /*
  2515. * this gets a little ugly. If reiserfs_get_block returned an
  2516. * error and left a transacstion running, we've got to close
  2517. * it, and we've got to free handle if it was a persistent
  2518. * transaction.
  2519. *
  2520. * But, if we had nested into an existing transaction, we need
  2521. * to just drop the ref count on the handle.
  2522. *
  2523. * If old_ref == 0, the transaction is from reiserfs_get_block,
  2524. * and it was a persistent trans. Otherwise, it was nested
  2525. * above.
  2526. */
  2527. if (th->t_refcount > old_ref) {
  2528. if (old_ref)
  2529. th->t_refcount--;
  2530. else {
  2531. int err;
  2532. reiserfs_write_lock(inode->i_sb);
  2533. err = reiserfs_end_persistent_transaction(th);
  2534. reiserfs_write_unlock(inode->i_sb);
  2535. if (err)
  2536. ret = err;
  2537. }
  2538. }
  2539. }
  2540. if (ret) {
  2541. unlock_page(page);
  2542. put_page(page);
  2543. /* Truncate allocated blocks */
  2544. reiserfs_truncate_failed_write(inode);
  2545. }
  2546. return ret;
  2547. }
  2548. int __reiserfs_write_begin(struct page *page, unsigned from, unsigned len)
  2549. {
  2550. struct inode *inode = page->mapping->host;
  2551. int ret;
  2552. int old_ref = 0;
  2553. int depth;
  2554. depth = reiserfs_write_unlock_nested(inode->i_sb);
  2555. reiserfs_wait_on_write_block(inode->i_sb);
  2556. reiserfs_write_lock_nested(inode->i_sb, depth);
  2557. fix_tail_page_for_writing(page);
  2558. if (reiserfs_transaction_running(inode->i_sb)) {
  2559. struct reiserfs_transaction_handle *th;
  2560. th = (struct reiserfs_transaction_handle *)current->
  2561. journal_info;
  2562. BUG_ON(!th->t_refcount);
  2563. BUG_ON(!th->t_trans_id);
  2564. old_ref = th->t_refcount;
  2565. th->t_refcount++;
  2566. }
  2567. ret = __block_write_begin(page, from, len, reiserfs_get_block);
  2568. if (ret && reiserfs_transaction_running(inode->i_sb)) {
  2569. struct reiserfs_transaction_handle *th = current->journal_info;
  2570. /*
  2571. * this gets a little ugly. If reiserfs_get_block returned an
  2572. * error and left a transacstion running, we've got to close
  2573. * it, and we've got to free handle if it was a persistent
  2574. * transaction.
  2575. *
  2576. * But, if we had nested into an existing transaction, we need
  2577. * to just drop the ref count on the handle.
  2578. *
  2579. * If old_ref == 0, the transaction is from reiserfs_get_block,
  2580. * and it was a persistent trans. Otherwise, it was nested
  2581. * above.
  2582. */
  2583. if (th->t_refcount > old_ref) {
  2584. if (old_ref)
  2585. th->t_refcount--;
  2586. else {
  2587. int err;
  2588. reiserfs_write_lock(inode->i_sb);
  2589. err = reiserfs_end_persistent_transaction(th);
  2590. reiserfs_write_unlock(inode->i_sb);
  2591. if (err)
  2592. ret = err;
  2593. }
  2594. }
  2595. }
  2596. return ret;
  2597. }
  2598. static sector_t reiserfs_aop_bmap(struct address_space *as, sector_t block)
  2599. {
  2600. return generic_block_bmap(as, block, reiserfs_bmap);
  2601. }
  2602. static int reiserfs_write_end(struct file *file, struct address_space *mapping,
  2603. loff_t pos, unsigned len, unsigned copied,
  2604. struct page *page, void *fsdata)
  2605. {
  2606. struct inode *inode = page->mapping->host;
  2607. int ret = 0;
  2608. int update_sd = 0;
  2609. struct reiserfs_transaction_handle *th;
  2610. unsigned start;
  2611. bool locked = false;
  2612. if ((unsigned long)fsdata & AOP_FLAG_CONT_EXPAND)
  2613. pos ++;
  2614. reiserfs_wait_on_write_block(inode->i_sb);
  2615. if (reiserfs_transaction_running(inode->i_sb))
  2616. th = current->journal_info;
  2617. else
  2618. th = NULL;
  2619. start = pos & (PAGE_SIZE - 1);
  2620. if (unlikely(copied < len)) {
  2621. if (!PageUptodate(page))
  2622. copied = 0;
  2623. page_zero_new_buffers(page, start + copied, start + len);
  2624. }
  2625. flush_dcache_page(page);
  2626. reiserfs_commit_page(inode, page, start, start + copied);
  2627. /*
  2628. * generic_commit_write does this for us, but does not update the
  2629. * transaction tracking stuff when the size changes. So, we have
  2630. * to do the i_size updates here.
  2631. */
  2632. if (pos + copied > inode->i_size) {
  2633. struct reiserfs_transaction_handle myth;
  2634. reiserfs_write_lock(inode->i_sb);
  2635. locked = true;
  2636. /*
  2637. * If the file have grown beyond the border where it
  2638. * can have a tail, unmark it as needing a tail
  2639. * packing
  2640. */
  2641. if ((have_large_tails(inode->i_sb)
  2642. && inode->i_size > i_block_size(inode) * 4)
  2643. || (have_small_tails(inode->i_sb)
  2644. && inode->i_size > i_block_size(inode)))
  2645. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  2646. ret = journal_begin(&myth, inode->i_sb, 1);
  2647. if (ret)
  2648. goto journal_error;
  2649. reiserfs_update_inode_transaction(inode);
  2650. inode->i_size = pos + copied;
  2651. /*
  2652. * this will just nest into our transaction. It's important
  2653. * to use mark_inode_dirty so the inode gets pushed around on
  2654. * the dirty lists, and so that O_SYNC works as expected
  2655. */
  2656. mark_inode_dirty(inode);
  2657. reiserfs_update_sd(&myth, inode);
  2658. update_sd = 1;
  2659. ret = journal_end(&myth);
  2660. if (ret)
  2661. goto journal_error;
  2662. }
  2663. if (th) {
  2664. if (!locked) {
  2665. reiserfs_write_lock(inode->i_sb);
  2666. locked = true;
  2667. }
  2668. if (!update_sd)
  2669. mark_inode_dirty(inode);
  2670. ret = reiserfs_end_persistent_transaction(th);
  2671. if (ret)
  2672. goto out;
  2673. }
  2674. out:
  2675. if (locked)
  2676. reiserfs_write_unlock(inode->i_sb);
  2677. unlock_page(page);
  2678. put_page(page);
  2679. if (pos + len > inode->i_size)
  2680. reiserfs_truncate_failed_write(inode);
  2681. return ret == 0 ? copied : ret;
  2682. journal_error:
  2683. reiserfs_write_unlock(inode->i_sb);
  2684. locked = false;
  2685. if (th) {
  2686. if (!update_sd)
  2687. reiserfs_update_sd(th, inode);
  2688. ret = reiserfs_end_persistent_transaction(th);
  2689. }
  2690. goto out;
  2691. }
  2692. int reiserfs_commit_write(struct file *f, struct page *page,
  2693. unsigned from, unsigned to)
  2694. {
  2695. struct inode *inode = page->mapping->host;
  2696. loff_t pos = ((loff_t) page->index << PAGE_SHIFT) + to;
  2697. int ret = 0;
  2698. int update_sd = 0;
  2699. struct reiserfs_transaction_handle *th = NULL;
  2700. int depth;
  2701. depth = reiserfs_write_unlock_nested(inode->i_sb);
  2702. reiserfs_wait_on_write_block(inode->i_sb);
  2703. reiserfs_write_lock_nested(inode->i_sb, depth);
  2704. if (reiserfs_transaction_running(inode->i_sb)) {
  2705. th = current->journal_info;
  2706. }
  2707. reiserfs_commit_page(inode, page, from, to);
  2708. /*
  2709. * generic_commit_write does this for us, but does not update the
  2710. * transaction tracking stuff when the size changes. So, we have
  2711. * to do the i_size updates here.
  2712. */
  2713. if (pos > inode->i_size) {
  2714. struct reiserfs_transaction_handle myth;
  2715. /*
  2716. * If the file have grown beyond the border where it
  2717. * can have a tail, unmark it as needing a tail
  2718. * packing
  2719. */
  2720. if ((have_large_tails(inode->i_sb)
  2721. && inode->i_size > i_block_size(inode) * 4)
  2722. || (have_small_tails(inode->i_sb)
  2723. && inode->i_size > i_block_size(inode)))
  2724. REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
  2725. ret = journal_begin(&myth, inode->i_sb, 1);
  2726. if (ret)
  2727. goto journal_error;
  2728. reiserfs_update_inode_transaction(inode);
  2729. inode->i_size = pos;
  2730. /*
  2731. * this will just nest into our transaction. It's important
  2732. * to use mark_inode_dirty so the inode gets pushed around
  2733. * on the dirty lists, and so that O_SYNC works as expected
  2734. */
  2735. mark_inode_dirty(inode);
  2736. reiserfs_update_sd(&myth, inode);
  2737. update_sd = 1;
  2738. ret = journal_end(&myth);
  2739. if (ret)
  2740. goto journal_error;
  2741. }
  2742. if (th) {
  2743. if (!update_sd)
  2744. mark_inode_dirty(inode);
  2745. ret = reiserfs_end_persistent_transaction(th);
  2746. if (ret)
  2747. goto out;
  2748. }
  2749. out:
  2750. return ret;
  2751. journal_error:
  2752. if (th) {
  2753. if (!update_sd)
  2754. reiserfs_update_sd(th, inode);
  2755. ret = reiserfs_end_persistent_transaction(th);
  2756. }
  2757. return ret;
  2758. }
  2759. void sd_attrs_to_i_attrs(__u16 sd_attrs, struct inode *inode)
  2760. {
  2761. if (reiserfs_attrs(inode->i_sb)) {
  2762. if (sd_attrs & REISERFS_SYNC_FL)
  2763. inode->i_flags |= S_SYNC;
  2764. else
  2765. inode->i_flags &= ~S_SYNC;
  2766. if (sd_attrs & REISERFS_IMMUTABLE_FL)
  2767. inode->i_flags |= S_IMMUTABLE;
  2768. else
  2769. inode->i_flags &= ~S_IMMUTABLE;
  2770. if (sd_attrs & REISERFS_APPEND_FL)
  2771. inode->i_flags |= S_APPEND;
  2772. else
  2773. inode->i_flags &= ~S_APPEND;
  2774. if (sd_attrs & REISERFS_NOATIME_FL)
  2775. inode->i_flags |= S_NOATIME;
  2776. else
  2777. inode->i_flags &= ~S_NOATIME;
  2778. if (sd_attrs & REISERFS_NOTAIL_FL)
  2779. REISERFS_I(inode)->i_flags |= i_nopack_mask;
  2780. else
  2781. REISERFS_I(inode)->i_flags &= ~i_nopack_mask;
  2782. }
  2783. }
  2784. /*
  2785. * decide if this buffer needs to stay around for data logging or ordered
  2786. * write purposes
  2787. */
  2788. static int invalidatepage_can_drop(struct inode *inode, struct buffer_head *bh)
  2789. {
  2790. int ret = 1;
  2791. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  2792. lock_buffer(bh);
  2793. spin_lock(&j->j_dirty_buffers_lock);
  2794. if (!buffer_mapped(bh)) {
  2795. goto free_jh;
  2796. }
  2797. /*
  2798. * the page is locked, and the only places that log a data buffer
  2799. * also lock the page.
  2800. */
  2801. if (reiserfs_file_data_log(inode)) {
  2802. /*
  2803. * very conservative, leave the buffer pinned if
  2804. * anyone might need it.
  2805. */
  2806. if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
  2807. ret = 0;
  2808. }
  2809. } else if (buffer_dirty(bh)) {
  2810. struct reiserfs_journal_list *jl;
  2811. struct reiserfs_jh *jh = bh->b_private;
  2812. /*
  2813. * why is this safe?
  2814. * reiserfs_setattr updates i_size in the on disk
  2815. * stat data before allowing vmtruncate to be called.
  2816. *
  2817. * If buffer was put onto the ordered list for this
  2818. * transaction, we know for sure either this transaction
  2819. * or an older one already has updated i_size on disk,
  2820. * and this ordered data won't be referenced in the file
  2821. * if we crash.
  2822. *
  2823. * if the buffer was put onto the ordered list for an older
  2824. * transaction, we need to leave it around
  2825. */
  2826. if (jh && (jl = jh->jl)
  2827. && jl != SB_JOURNAL(inode->i_sb)->j_current_jl)
  2828. ret = 0;
  2829. }
  2830. free_jh:
  2831. if (ret && bh->b_private) {
  2832. reiserfs_free_jh(bh);
  2833. }
  2834. spin_unlock(&j->j_dirty_buffers_lock);
  2835. unlock_buffer(bh);
  2836. return ret;
  2837. }
  2838. /* clm -- taken from fs/buffer.c:block_invalidate_page */
  2839. static void reiserfs_invalidatepage(struct page *page, unsigned int offset,
  2840. unsigned int length)
  2841. {
  2842. struct buffer_head *head, *bh, *next;
  2843. struct inode *inode = page->mapping->host;
  2844. unsigned int curr_off = 0;
  2845. unsigned int stop = offset + length;
  2846. int partial_page = (offset || length < PAGE_SIZE);
  2847. int ret = 1;
  2848. BUG_ON(!PageLocked(page));
  2849. if (!partial_page)
  2850. ClearPageChecked(page);
  2851. if (!page_has_buffers(page))
  2852. goto out;
  2853. head = page_buffers(page);
  2854. bh = head;
  2855. do {
  2856. unsigned int next_off = curr_off + bh->b_size;
  2857. next = bh->b_this_page;
  2858. if (next_off > stop)
  2859. goto out;
  2860. /*
  2861. * is this block fully invalidated?
  2862. */
  2863. if (offset <= curr_off) {
  2864. if (invalidatepage_can_drop(inode, bh))
  2865. reiserfs_unmap_buffer(bh);
  2866. else
  2867. ret = 0;
  2868. }
  2869. curr_off = next_off;
  2870. bh = next;
  2871. } while (bh != head);
  2872. /*
  2873. * We release buffers only if the entire page is being invalidated.
  2874. * The get_block cached value has been unconditionally invalidated,
  2875. * so real IO is not possible anymore.
  2876. */
  2877. if (!partial_page && ret) {
  2878. ret = try_to_release_page(page, 0);
  2879. /* maybe should BUG_ON(!ret); - neilb */
  2880. }
  2881. out:
  2882. return;
  2883. }
  2884. static int reiserfs_set_page_dirty(struct page *page)
  2885. {
  2886. struct inode *inode = page->mapping->host;
  2887. if (reiserfs_file_data_log(inode)) {
  2888. SetPageChecked(page);
  2889. return __set_page_dirty_nobuffers(page);
  2890. }
  2891. return __set_page_dirty_buffers(page);
  2892. }
  2893. /*
  2894. * Returns 1 if the page's buffers were dropped. The page is locked.
  2895. *
  2896. * Takes j_dirty_buffers_lock to protect the b_assoc_buffers list_heads
  2897. * in the buffers at page_buffers(page).
  2898. *
  2899. * even in -o notail mode, we can't be sure an old mount without -o notail
  2900. * didn't create files with tails.
  2901. */
  2902. static int reiserfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
  2903. {
  2904. struct inode *inode = page->mapping->host;
  2905. struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
  2906. struct buffer_head *head;
  2907. struct buffer_head *bh;
  2908. int ret = 1;
  2909. WARN_ON(PageChecked(page));
  2910. spin_lock(&j->j_dirty_buffers_lock);
  2911. head = page_buffers(page);
  2912. bh = head;
  2913. do {
  2914. if (bh->b_private) {
  2915. if (!buffer_dirty(bh) && !buffer_locked(bh)) {
  2916. reiserfs_free_jh(bh);
  2917. } else {
  2918. ret = 0;
  2919. break;
  2920. }
  2921. }
  2922. bh = bh->b_this_page;
  2923. } while (bh != head);
  2924. if (ret)
  2925. ret = try_to_free_buffers(page);
  2926. spin_unlock(&j->j_dirty_buffers_lock);
  2927. return ret;
  2928. }
  2929. /*
  2930. * We thank Mingming Cao for helping us understand in great detail what
  2931. * to do in this section of the code.
  2932. */
  2933. static ssize_t reiserfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  2934. {
  2935. struct file *file = iocb->ki_filp;
  2936. struct inode *inode = file->f_mapping->host;
  2937. size_t count = iov_iter_count(iter);
  2938. ssize_t ret;
  2939. ret = blockdev_direct_IO(iocb, inode, iter,
  2940. reiserfs_get_blocks_direct_io);
  2941. /*
  2942. * In case of error extending write may have instantiated a few
  2943. * blocks outside i_size. Trim these off again.
  2944. */
  2945. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  2946. loff_t isize = i_size_read(inode);
  2947. loff_t end = iocb->ki_pos + count;
  2948. if ((end > isize) && inode_newsize_ok(inode, isize) == 0) {
  2949. truncate_setsize(inode, isize);
  2950. reiserfs_vfs_truncate_file(inode);
  2951. }
  2952. }
  2953. return ret;
  2954. }
  2955. int reiserfs_setattr(struct dentry *dentry, struct iattr *attr)
  2956. {
  2957. struct inode *inode = d_inode(dentry);
  2958. unsigned int ia_valid;
  2959. int error;
  2960. error = setattr_prepare(dentry, attr);
  2961. if (error)
  2962. return error;
  2963. /* must be turned off for recursive notify_change calls */
  2964. ia_valid = attr->ia_valid &= ~(ATTR_KILL_SUID|ATTR_KILL_SGID);
  2965. if (is_quota_modification(inode, attr)) {
  2966. error = dquot_initialize(inode);
  2967. if (error)
  2968. return error;
  2969. }
  2970. reiserfs_write_lock(inode->i_sb);
  2971. if (attr->ia_valid & ATTR_SIZE) {
  2972. /*
  2973. * version 2 items will be caught by the s_maxbytes check
  2974. * done for us in vmtruncate
  2975. */
  2976. if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5 &&
  2977. attr->ia_size > MAX_NON_LFS) {
  2978. reiserfs_write_unlock(inode->i_sb);
  2979. error = -EFBIG;
  2980. goto out;
  2981. }
  2982. inode_dio_wait(inode);
  2983. /* fill in hole pointers in the expanding truncate case. */
  2984. if (attr->ia_size > inode->i_size) {
  2985. error = generic_cont_expand_simple(inode, attr->ia_size);
  2986. if (REISERFS_I(inode)->i_prealloc_count > 0) {
  2987. int err;
  2988. struct reiserfs_transaction_handle th;
  2989. /* we're changing at most 2 bitmaps, inode + super */
  2990. err = journal_begin(&th, inode->i_sb, 4);
  2991. if (!err) {
  2992. reiserfs_discard_prealloc(&th, inode);
  2993. err = journal_end(&th);
  2994. }
  2995. if (err)
  2996. error = err;
  2997. }
  2998. if (error) {
  2999. reiserfs_write_unlock(inode->i_sb);
  3000. goto out;
  3001. }
  3002. /*
  3003. * file size is changed, ctime and mtime are
  3004. * to be updated
  3005. */
  3006. attr->ia_valid |= (ATTR_MTIME | ATTR_CTIME);
  3007. }
  3008. }
  3009. reiserfs_write_unlock(inode->i_sb);
  3010. if ((((attr->ia_valid & ATTR_UID) && (from_kuid(&init_user_ns, attr->ia_uid) & ~0xffff)) ||
  3011. ((attr->ia_valid & ATTR_GID) && (from_kgid(&init_user_ns, attr->ia_gid) & ~0xffff))) &&
  3012. (get_inode_sd_version(inode) == STAT_DATA_V1)) {
  3013. /* stat data of format v3.5 has 16 bit uid and gid */
  3014. error = -EINVAL;
  3015. goto out;
  3016. }
  3017. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  3018. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  3019. struct reiserfs_transaction_handle th;
  3020. int jbegin_count =
  3021. 2 *
  3022. (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
  3023. REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb)) +
  3024. 2;
  3025. error = reiserfs_chown_xattrs(inode, attr);
  3026. if (error)
  3027. return error;
  3028. /*
  3029. * (user+group)*(old+new) structure - we count quota
  3030. * info and , inode write (sb, inode)
  3031. */
  3032. reiserfs_write_lock(inode->i_sb);
  3033. error = journal_begin(&th, inode->i_sb, jbegin_count);
  3034. reiserfs_write_unlock(inode->i_sb);
  3035. if (error)
  3036. goto out;
  3037. error = dquot_transfer(inode, attr);
  3038. reiserfs_write_lock(inode->i_sb);
  3039. if (error) {
  3040. journal_end(&th);
  3041. reiserfs_write_unlock(inode->i_sb);
  3042. goto out;
  3043. }
  3044. /*
  3045. * Update corresponding info in inode so that everything
  3046. * is in one transaction
  3047. */
  3048. if (attr->ia_valid & ATTR_UID)
  3049. inode->i_uid = attr->ia_uid;
  3050. if (attr->ia_valid & ATTR_GID)
  3051. inode->i_gid = attr->ia_gid;
  3052. mark_inode_dirty(inode);
  3053. error = journal_end(&th);
  3054. reiserfs_write_unlock(inode->i_sb);
  3055. if (error)
  3056. goto out;
  3057. }
  3058. if ((attr->ia_valid & ATTR_SIZE) &&
  3059. attr->ia_size != i_size_read(inode)) {
  3060. error = inode_newsize_ok(inode, attr->ia_size);
  3061. if (!error) {
  3062. /*
  3063. * Could race against reiserfs_file_release
  3064. * if called from NFS, so take tailpack mutex.
  3065. */
  3066. mutex_lock(&REISERFS_I(inode)->tailpack);
  3067. truncate_setsize(inode, attr->ia_size);
  3068. reiserfs_truncate_file(inode, 1);
  3069. mutex_unlock(&REISERFS_I(inode)->tailpack);
  3070. }
  3071. }
  3072. if (!error) {
  3073. setattr_copy(inode, attr);
  3074. mark_inode_dirty(inode);
  3075. }
  3076. if (!error && reiserfs_posixacl(inode->i_sb)) {
  3077. if (attr->ia_valid & ATTR_MODE)
  3078. error = reiserfs_acl_chmod(inode);
  3079. }
  3080. out:
  3081. return error;
  3082. }
  3083. const struct address_space_operations reiserfs_address_space_operations = {
  3084. .writepage = reiserfs_writepage,
  3085. .readpage = reiserfs_readpage,
  3086. .readpages = reiserfs_readpages,
  3087. .releasepage = reiserfs_releasepage,
  3088. .invalidatepage = reiserfs_invalidatepage,
  3089. .write_begin = reiserfs_write_begin,
  3090. .write_end = reiserfs_write_end,
  3091. .bmap = reiserfs_aop_bmap,
  3092. .direct_IO = reiserfs_direct_IO,
  3093. .set_page_dirty = reiserfs_set_page_dirty,
  3094. };