inode.c 93 KB

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