inode.c 92 KB

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