inode.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. * linux/fs/ext2/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@dcs.ed.ac.uk), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/time.h>
  25. #include <linux/highuid.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/dax.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/quotaops.h>
  30. #include <linux/writeback.h>
  31. #include <linux/buffer_head.h>
  32. #include <linux/mpage.h>
  33. #include <linux/fiemap.h>
  34. #include <linux/iomap.h>
  35. #include <linux/namei.h>
  36. #include <linux/uio.h>
  37. #include "ext2.h"
  38. #include "acl.h"
  39. #include "xattr.h"
  40. static int __ext2_write_inode(struct inode *inode, int do_sync);
  41. /*
  42. * Test whether an inode is a fast symlink.
  43. */
  44. static inline int ext2_inode_is_fast_symlink(struct inode *inode)
  45. {
  46. int ea_blocks = EXT2_I(inode)->i_file_acl ?
  47. (inode->i_sb->s_blocksize >> 9) : 0;
  48. return (S_ISLNK(inode->i_mode) &&
  49. inode->i_blocks - ea_blocks == 0);
  50. }
  51. static void ext2_truncate_blocks(struct inode *inode, loff_t offset);
  52. static void ext2_write_failed(struct address_space *mapping, loff_t to)
  53. {
  54. struct inode *inode = mapping->host;
  55. if (to > inode->i_size) {
  56. truncate_pagecache(inode, inode->i_size);
  57. ext2_truncate_blocks(inode, inode->i_size);
  58. }
  59. }
  60. /*
  61. * Called at the last iput() if i_nlink is zero.
  62. */
  63. void ext2_evict_inode(struct inode * inode)
  64. {
  65. struct ext2_block_alloc_info *rsv;
  66. int want_delete = 0;
  67. if (!inode->i_nlink && !is_bad_inode(inode)) {
  68. want_delete = 1;
  69. dquot_initialize(inode);
  70. } else {
  71. dquot_drop(inode);
  72. }
  73. truncate_inode_pages_final(&inode->i_data);
  74. if (want_delete) {
  75. sb_start_intwrite(inode->i_sb);
  76. /* set dtime */
  77. EXT2_I(inode)->i_dtime = get_seconds();
  78. mark_inode_dirty(inode);
  79. __ext2_write_inode(inode, inode_needs_sync(inode));
  80. /* truncate to 0 */
  81. inode->i_size = 0;
  82. if (inode->i_blocks)
  83. ext2_truncate_blocks(inode, 0);
  84. ext2_xattr_delete_inode(inode);
  85. }
  86. invalidate_inode_buffers(inode);
  87. clear_inode(inode);
  88. ext2_discard_reservation(inode);
  89. rsv = EXT2_I(inode)->i_block_alloc_info;
  90. EXT2_I(inode)->i_block_alloc_info = NULL;
  91. if (unlikely(rsv))
  92. kfree(rsv);
  93. if (want_delete) {
  94. ext2_free_inode(inode);
  95. sb_end_intwrite(inode->i_sb);
  96. }
  97. }
  98. typedef struct {
  99. __le32 *p;
  100. __le32 key;
  101. struct buffer_head *bh;
  102. } Indirect;
  103. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  104. {
  105. p->key = *(p->p = v);
  106. p->bh = bh;
  107. }
  108. static inline int verify_chain(Indirect *from, Indirect *to)
  109. {
  110. while (from <= to && from->key == *from->p)
  111. from++;
  112. return (from > to);
  113. }
  114. /**
  115. * ext2_block_to_path - parse the block number into array of offsets
  116. * @inode: inode in question (we are only interested in its superblock)
  117. * @i_block: block number to be parsed
  118. * @offsets: array to store the offsets in
  119. * @boundary: set this non-zero if the referred-to block is likely to be
  120. * followed (on disk) by an indirect block.
  121. * To store the locations of file's data ext2 uses a data structure common
  122. * for UNIX filesystems - tree of pointers anchored in the inode, with
  123. * data blocks at leaves and indirect blocks in intermediate nodes.
  124. * This function translates the block number into path in that tree -
  125. * return value is the path length and @offsets[n] is the offset of
  126. * pointer to (n+1)th node in the nth one. If @block is out of range
  127. * (negative or too large) warning is printed and zero returned.
  128. *
  129. * Note: function doesn't find node addresses, so no IO is needed. All
  130. * we need to know is the capacity of indirect blocks (taken from the
  131. * inode->i_sb).
  132. */
  133. /*
  134. * Portability note: the last comparison (check that we fit into triple
  135. * indirect block) is spelled differently, because otherwise on an
  136. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  137. * if our filesystem had 8Kb blocks. We might use long long, but that would
  138. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  139. * i_block would have to be negative in the very beginning, so we would not
  140. * get there at all.
  141. */
  142. static int ext2_block_to_path(struct inode *inode,
  143. long i_block, int offsets[4], int *boundary)
  144. {
  145. int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  146. int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
  147. const long direct_blocks = EXT2_NDIR_BLOCKS,
  148. indirect_blocks = ptrs,
  149. double_blocks = (1 << (ptrs_bits * 2));
  150. int n = 0;
  151. int final = 0;
  152. if (i_block < 0) {
  153. ext2_msg(inode->i_sb, KERN_WARNING,
  154. "warning: %s: block < 0", __func__);
  155. } else if (i_block < direct_blocks) {
  156. offsets[n++] = i_block;
  157. final = direct_blocks;
  158. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  159. offsets[n++] = EXT2_IND_BLOCK;
  160. offsets[n++] = i_block;
  161. final = ptrs;
  162. } else if ((i_block -= indirect_blocks) < double_blocks) {
  163. offsets[n++] = EXT2_DIND_BLOCK;
  164. offsets[n++] = i_block >> ptrs_bits;
  165. offsets[n++] = i_block & (ptrs - 1);
  166. final = ptrs;
  167. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  168. offsets[n++] = EXT2_TIND_BLOCK;
  169. offsets[n++] = i_block >> (ptrs_bits * 2);
  170. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  171. offsets[n++] = i_block & (ptrs - 1);
  172. final = ptrs;
  173. } else {
  174. ext2_msg(inode->i_sb, KERN_WARNING,
  175. "warning: %s: block is too big", __func__);
  176. }
  177. if (boundary)
  178. *boundary = final - 1 - (i_block & (ptrs - 1));
  179. return n;
  180. }
  181. /**
  182. * ext2_get_branch - read the chain of indirect blocks leading to data
  183. * @inode: inode in question
  184. * @depth: depth of the chain (1 - direct pointer, etc.)
  185. * @offsets: offsets of pointers in inode/indirect blocks
  186. * @chain: place to store the result
  187. * @err: here we store the error value
  188. *
  189. * Function fills the array of triples <key, p, bh> and returns %NULL
  190. * if everything went OK or the pointer to the last filled triple
  191. * (incomplete one) otherwise. Upon the return chain[i].key contains
  192. * the number of (i+1)-th block in the chain (as it is stored in memory,
  193. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  194. * number (it points into struct inode for i==0 and into the bh->b_data
  195. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  196. * block for i>0 and NULL for i==0. In other words, it holds the block
  197. * numbers of the chain, addresses they were taken from (and where we can
  198. * verify that chain did not change) and buffer_heads hosting these
  199. * numbers.
  200. *
  201. * Function stops when it stumbles upon zero pointer (absent block)
  202. * (pointer to last triple returned, *@err == 0)
  203. * or when it gets an IO error reading an indirect block
  204. * (ditto, *@err == -EIO)
  205. * or when it notices that chain had been changed while it was reading
  206. * (ditto, *@err == -EAGAIN)
  207. * or when it reads all @depth-1 indirect blocks successfully and finds
  208. * the whole chain, all way to the data (returns %NULL, *err == 0).
  209. */
  210. static Indirect *ext2_get_branch(struct inode *inode,
  211. int depth,
  212. int *offsets,
  213. Indirect chain[4],
  214. int *err)
  215. {
  216. struct super_block *sb = inode->i_sb;
  217. Indirect *p = chain;
  218. struct buffer_head *bh;
  219. *err = 0;
  220. /* i_data is not going away, no lock needed */
  221. add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
  222. if (!p->key)
  223. goto no_block;
  224. while (--depth) {
  225. bh = sb_bread(sb, le32_to_cpu(p->key));
  226. if (!bh)
  227. goto failure;
  228. read_lock(&EXT2_I(inode)->i_meta_lock);
  229. if (!verify_chain(chain, p))
  230. goto changed;
  231. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  232. read_unlock(&EXT2_I(inode)->i_meta_lock);
  233. if (!p->key)
  234. goto no_block;
  235. }
  236. return NULL;
  237. changed:
  238. read_unlock(&EXT2_I(inode)->i_meta_lock);
  239. brelse(bh);
  240. *err = -EAGAIN;
  241. goto no_block;
  242. failure:
  243. *err = -EIO;
  244. no_block:
  245. return p;
  246. }
  247. /**
  248. * ext2_find_near - find a place for allocation with sufficient locality
  249. * @inode: owner
  250. * @ind: descriptor of indirect block.
  251. *
  252. * This function returns the preferred place for block allocation.
  253. * It is used when heuristic for sequential allocation fails.
  254. * Rules are:
  255. * + if there is a block to the left of our position - allocate near it.
  256. * + if pointer will live in indirect block - allocate near that block.
  257. * + if pointer will live in inode - allocate in the same cylinder group.
  258. *
  259. * In the latter case we colour the starting block by the callers PID to
  260. * prevent it from clashing with concurrent allocations for a different inode
  261. * in the same block group. The PID is used here so that functionally related
  262. * files will be close-by on-disk.
  263. *
  264. * Caller must make sure that @ind is valid and will stay that way.
  265. */
  266. static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
  267. {
  268. struct ext2_inode_info *ei = EXT2_I(inode);
  269. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  270. __le32 *p;
  271. ext2_fsblk_t bg_start;
  272. ext2_fsblk_t colour;
  273. /* Try to find previous block */
  274. for (p = ind->p - 1; p >= start; p--)
  275. if (*p)
  276. return le32_to_cpu(*p);
  277. /* No such thing, so let's try location of indirect block */
  278. if (ind->bh)
  279. return ind->bh->b_blocknr;
  280. /*
  281. * It is going to be referred from inode itself? OK, just put it into
  282. * the same cylinder group then.
  283. */
  284. bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
  285. colour = (current->pid % 16) *
  286. (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  287. return bg_start + colour;
  288. }
  289. /**
  290. * ext2_find_goal - find a preferred place for allocation.
  291. * @inode: owner
  292. * @block: block we want
  293. * @partial: pointer to the last triple within a chain
  294. *
  295. * Returns preferred place for a block (the goal).
  296. */
  297. static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
  298. Indirect *partial)
  299. {
  300. struct ext2_block_alloc_info *block_i;
  301. block_i = EXT2_I(inode)->i_block_alloc_info;
  302. /*
  303. * try the heuristic for sequential allocation,
  304. * failing that at least try to get decent locality.
  305. */
  306. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  307. && (block_i->last_alloc_physical_block != 0)) {
  308. return block_i->last_alloc_physical_block + 1;
  309. }
  310. return ext2_find_near(inode, partial);
  311. }
  312. /**
  313. * ext2_blks_to_allocate: Look up the block map and count the number
  314. * of direct blocks need to be allocated for the given branch.
  315. *
  316. * @branch: chain of indirect blocks
  317. * @k: number of blocks need for indirect blocks
  318. * @blks: number of data blocks to be mapped.
  319. * @blocks_to_boundary: the offset in the indirect block
  320. *
  321. * return the total number of blocks to be allocate, including the
  322. * direct and indirect blocks.
  323. */
  324. static int
  325. ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
  326. int blocks_to_boundary)
  327. {
  328. unsigned long count = 0;
  329. /*
  330. * Simple case, [t,d]Indirect block(s) has not allocated yet
  331. * then it's clear blocks on that path have not allocated
  332. */
  333. if (k > 0) {
  334. /* right now don't hanel cross boundary allocation */
  335. if (blks < blocks_to_boundary + 1)
  336. count += blks;
  337. else
  338. count += blocks_to_boundary + 1;
  339. return count;
  340. }
  341. count++;
  342. while (count < blks && count <= blocks_to_boundary
  343. && le32_to_cpu(*(branch[0].p + count)) == 0) {
  344. count++;
  345. }
  346. return count;
  347. }
  348. /**
  349. * ext2_alloc_blocks: multiple allocate blocks needed for a branch
  350. * @indirect_blks: the number of blocks need to allocate for indirect
  351. * blocks
  352. *
  353. * @new_blocks: on return it will store the new block numbers for
  354. * the indirect blocks(if needed) and the first direct block,
  355. * @blks: on return it will store the total number of allocated
  356. * direct blocks
  357. */
  358. static int ext2_alloc_blocks(struct inode *inode,
  359. ext2_fsblk_t goal, int indirect_blks, int blks,
  360. ext2_fsblk_t new_blocks[4], int *err)
  361. {
  362. int target, i;
  363. unsigned long count = 0;
  364. int index = 0;
  365. ext2_fsblk_t current_block = 0;
  366. int ret = 0;
  367. /*
  368. * Here we try to allocate the requested multiple blocks at once,
  369. * on a best-effort basis.
  370. * To build a branch, we should allocate blocks for
  371. * the indirect blocks(if not allocated yet), and at least
  372. * the first direct block of this branch. That's the
  373. * minimum number of blocks need to allocate(required)
  374. */
  375. target = blks + indirect_blks;
  376. while (1) {
  377. count = target;
  378. /* allocating blocks for indirect blocks and direct blocks */
  379. current_block = ext2_new_blocks(inode,goal,&count,err);
  380. if (*err)
  381. goto failed_out;
  382. target -= count;
  383. /* allocate blocks for indirect blocks */
  384. while (index < indirect_blks && count) {
  385. new_blocks[index++] = current_block++;
  386. count--;
  387. }
  388. if (count > 0)
  389. break;
  390. }
  391. /* save the new block number for the first direct block */
  392. new_blocks[index] = current_block;
  393. /* total number of blocks allocated for direct blocks */
  394. ret = count;
  395. *err = 0;
  396. return ret;
  397. failed_out:
  398. for (i = 0; i <index; i++)
  399. ext2_free_blocks(inode, new_blocks[i], 1);
  400. if (index)
  401. mark_inode_dirty(inode);
  402. return ret;
  403. }
  404. /**
  405. * ext2_alloc_branch - allocate and set up a chain of blocks.
  406. * @inode: owner
  407. * @num: depth of the chain (number of blocks to allocate)
  408. * @offsets: offsets (in the blocks) to store the pointers to next.
  409. * @branch: place to store the chain in.
  410. *
  411. * This function allocates @num blocks, zeroes out all but the last one,
  412. * links them into chain and (if we are synchronous) writes them to disk.
  413. * In other words, it prepares a branch that can be spliced onto the
  414. * inode. It stores the information about that chain in the branch[], in
  415. * the same format as ext2_get_branch() would do. We are calling it after
  416. * we had read the existing part of chain and partial points to the last
  417. * triple of that (one with zero ->key). Upon the exit we have the same
  418. * picture as after the successful ext2_get_block(), except that in one
  419. * place chain is disconnected - *branch->p is still zero (we did not
  420. * set the last link), but branch->key contains the number that should
  421. * be placed into *branch->p to fill that gap.
  422. *
  423. * If allocation fails we free all blocks we've allocated (and forget
  424. * their buffer_heads) and return the error value the from failed
  425. * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  426. * as described above and return 0.
  427. */
  428. static int ext2_alloc_branch(struct inode *inode,
  429. int indirect_blks, int *blks, ext2_fsblk_t goal,
  430. int *offsets, Indirect *branch)
  431. {
  432. int blocksize = inode->i_sb->s_blocksize;
  433. int i, n = 0;
  434. int err = 0;
  435. struct buffer_head *bh;
  436. int num;
  437. ext2_fsblk_t new_blocks[4];
  438. ext2_fsblk_t current_block;
  439. num = ext2_alloc_blocks(inode, goal, indirect_blks,
  440. *blks, new_blocks, &err);
  441. if (err)
  442. return err;
  443. branch[0].key = cpu_to_le32(new_blocks[0]);
  444. /*
  445. * metadata blocks and data blocks are allocated.
  446. */
  447. for (n = 1; n <= indirect_blks; n++) {
  448. /*
  449. * Get buffer_head for parent block, zero it out
  450. * and set the pointer to new one, then send
  451. * parent to disk.
  452. */
  453. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  454. if (unlikely(!bh)) {
  455. err = -ENOMEM;
  456. goto failed;
  457. }
  458. branch[n].bh = bh;
  459. lock_buffer(bh);
  460. memset(bh->b_data, 0, blocksize);
  461. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  462. branch[n].key = cpu_to_le32(new_blocks[n]);
  463. *branch[n].p = branch[n].key;
  464. if ( n == indirect_blks) {
  465. current_block = new_blocks[n];
  466. /*
  467. * End of chain, update the last new metablock of
  468. * the chain to point to the new allocated
  469. * data blocks numbers
  470. */
  471. for (i=1; i < num; i++)
  472. *(branch[n].p + i) = cpu_to_le32(++current_block);
  473. }
  474. set_buffer_uptodate(bh);
  475. unlock_buffer(bh);
  476. mark_buffer_dirty_inode(bh, inode);
  477. /* We used to sync bh here if IS_SYNC(inode).
  478. * But we now rely upon generic_write_sync()
  479. * and b_inode_buffers. But not for directories.
  480. */
  481. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  482. sync_dirty_buffer(bh);
  483. }
  484. *blks = num;
  485. return err;
  486. failed:
  487. for (i = 1; i < n; i++)
  488. bforget(branch[i].bh);
  489. for (i = 0; i < indirect_blks; i++)
  490. ext2_free_blocks(inode, new_blocks[i], 1);
  491. ext2_free_blocks(inode, new_blocks[i], num);
  492. return err;
  493. }
  494. /**
  495. * ext2_splice_branch - splice the allocated branch onto inode.
  496. * @inode: owner
  497. * @block: (logical) number of block we are adding
  498. * @where: location of missing link
  499. * @num: number of indirect blocks we are adding
  500. * @blks: number of direct blocks we are adding
  501. *
  502. * This function fills the missing link and does all housekeeping needed in
  503. * inode (->i_blocks, etc.). In case of success we end up with the full
  504. * chain to new block and return 0.
  505. */
  506. static void ext2_splice_branch(struct inode *inode,
  507. long block, Indirect *where, int num, int blks)
  508. {
  509. int i;
  510. struct ext2_block_alloc_info *block_i;
  511. ext2_fsblk_t current_block;
  512. block_i = EXT2_I(inode)->i_block_alloc_info;
  513. /* XXX LOCKING probably should have i_meta_lock ?*/
  514. /* That's it */
  515. *where->p = where->key;
  516. /*
  517. * Update the host buffer_head or inode to point to more just allocated
  518. * direct blocks blocks
  519. */
  520. if (num == 0 && blks > 1) {
  521. current_block = le32_to_cpu(where->key) + 1;
  522. for (i = 1; i < blks; i++)
  523. *(where->p + i ) = cpu_to_le32(current_block++);
  524. }
  525. /*
  526. * update the most recently allocated logical & physical block
  527. * in i_block_alloc_info, to assist find the proper goal block for next
  528. * allocation
  529. */
  530. if (block_i) {
  531. block_i->last_alloc_logical_block = block + blks - 1;
  532. block_i->last_alloc_physical_block =
  533. le32_to_cpu(where[num].key) + blks - 1;
  534. }
  535. /* We are done with atomic stuff, now do the rest of housekeeping */
  536. /* had we spliced it onto indirect block? */
  537. if (where->bh)
  538. mark_buffer_dirty_inode(where->bh, inode);
  539. inode->i_ctime = current_time(inode);
  540. mark_inode_dirty(inode);
  541. }
  542. /*
  543. * Allocation strategy is simple: if we have to allocate something, we will
  544. * have to go the whole way to leaf. So let's do it before attaching anything
  545. * to tree, set linkage between the newborn blocks, write them if sync is
  546. * required, recheck the path, free and repeat if check fails, otherwise
  547. * set the last missing link (that will protect us from any truncate-generated
  548. * removals - all blocks on the path are immune now) and possibly force the
  549. * write on the parent block.
  550. * That has a nice additional property: no special recovery from the failed
  551. * allocations is needed - we simply release blocks and do not touch anything
  552. * reachable from inode.
  553. *
  554. * `handle' can be NULL if create == 0.
  555. *
  556. * return > 0, # of blocks mapped or allocated.
  557. * return = 0, if plain lookup failed.
  558. * return < 0, error case.
  559. */
  560. static int ext2_get_blocks(struct inode *inode,
  561. sector_t iblock, unsigned long maxblocks,
  562. u32 *bno, bool *new, bool *boundary,
  563. int create)
  564. {
  565. int err;
  566. int offsets[4];
  567. Indirect chain[4];
  568. Indirect *partial;
  569. ext2_fsblk_t goal;
  570. int indirect_blks;
  571. int blocks_to_boundary = 0;
  572. int depth;
  573. struct ext2_inode_info *ei = EXT2_I(inode);
  574. int count = 0;
  575. ext2_fsblk_t first_block = 0;
  576. BUG_ON(maxblocks == 0);
  577. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  578. if (depth == 0)
  579. return -EIO;
  580. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  581. /* Simplest case - block found, no allocation needed */
  582. if (!partial) {
  583. first_block = le32_to_cpu(chain[depth - 1].key);
  584. count++;
  585. /*map more blocks*/
  586. while (count < maxblocks && count <= blocks_to_boundary) {
  587. ext2_fsblk_t blk;
  588. if (!verify_chain(chain, chain + depth - 1)) {
  589. /*
  590. * Indirect block might be removed by
  591. * truncate while we were reading it.
  592. * Handling of that case: forget what we've
  593. * got now, go to reread.
  594. */
  595. err = -EAGAIN;
  596. count = 0;
  597. break;
  598. }
  599. blk = le32_to_cpu(*(chain[depth-1].p + count));
  600. if (blk == first_block + count)
  601. count++;
  602. else
  603. break;
  604. }
  605. if (err != -EAGAIN)
  606. goto got_it;
  607. }
  608. /* Next simple case - plain lookup or failed read of indirect block */
  609. if (!create || err == -EIO)
  610. goto cleanup;
  611. mutex_lock(&ei->truncate_mutex);
  612. /*
  613. * If the indirect block is missing while we are reading
  614. * the chain(ext2_get_branch() returns -EAGAIN err), or
  615. * if the chain has been changed after we grab the semaphore,
  616. * (either because another process truncated this branch, or
  617. * another get_block allocated this branch) re-grab the chain to see if
  618. * the request block has been allocated or not.
  619. *
  620. * Since we already block the truncate/other get_block
  621. * at this point, we will have the current copy of the chain when we
  622. * splice the branch into the tree.
  623. */
  624. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  625. while (partial > chain) {
  626. brelse(partial->bh);
  627. partial--;
  628. }
  629. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  630. if (!partial) {
  631. count++;
  632. mutex_unlock(&ei->truncate_mutex);
  633. if (err)
  634. goto cleanup;
  635. goto got_it;
  636. }
  637. }
  638. /*
  639. * Okay, we need to do block allocation. Lazily initialize the block
  640. * allocation info here if necessary
  641. */
  642. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  643. ext2_init_block_alloc_info(inode);
  644. goal = ext2_find_goal(inode, iblock, partial);
  645. /* the number of blocks need to allocate for [d,t]indirect blocks */
  646. indirect_blks = (chain + depth) - partial - 1;
  647. /*
  648. * Next look up the indirect map to count the totoal number of
  649. * direct blocks to allocate for this branch.
  650. */
  651. count = ext2_blks_to_allocate(partial, indirect_blks,
  652. maxblocks, blocks_to_boundary);
  653. /*
  654. * XXX ???? Block out ext2_truncate while we alter the tree
  655. */
  656. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  657. offsets + (partial - chain), partial);
  658. if (err) {
  659. mutex_unlock(&ei->truncate_mutex);
  660. goto cleanup;
  661. }
  662. if (IS_DAX(inode)) {
  663. int i;
  664. /*
  665. * We must unmap blocks before zeroing so that writeback cannot
  666. * overwrite zeros with stale data from block device page cache.
  667. */
  668. for (i = 0; i < count; i++) {
  669. unmap_underlying_metadata(inode->i_sb->s_bdev,
  670. le32_to_cpu(chain[depth-1].key) + i);
  671. }
  672. /*
  673. * block must be initialised before we put it in the tree
  674. * so that it's not found by another thread before it's
  675. * initialised
  676. */
  677. err = sb_issue_zeroout(inode->i_sb,
  678. le32_to_cpu(chain[depth-1].key), count,
  679. GFP_NOFS);
  680. if (err) {
  681. mutex_unlock(&ei->truncate_mutex);
  682. goto cleanup;
  683. }
  684. } else {
  685. *new = true;
  686. }
  687. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  688. mutex_unlock(&ei->truncate_mutex);
  689. got_it:
  690. if (count > blocks_to_boundary)
  691. *boundary = true;
  692. err = count;
  693. /* Clean up and exit */
  694. partial = chain + depth - 1; /* the whole chain */
  695. cleanup:
  696. while (partial > chain) {
  697. brelse(partial->bh);
  698. partial--;
  699. }
  700. if (err > 0)
  701. *bno = le32_to_cpu(chain[depth-1].key);
  702. return err;
  703. }
  704. int ext2_get_block(struct inode *inode, sector_t iblock,
  705. struct buffer_head *bh_result, int create)
  706. {
  707. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  708. bool new = false, boundary = false;
  709. u32 bno;
  710. int ret;
  711. ret = ext2_get_blocks(inode, iblock, max_blocks, &bno, &new, &boundary,
  712. create);
  713. if (ret <= 0)
  714. return ret;
  715. map_bh(bh_result, inode->i_sb, bno);
  716. bh_result->b_size = (ret << inode->i_blkbits);
  717. if (new)
  718. set_buffer_new(bh_result);
  719. if (boundary)
  720. set_buffer_boundary(bh_result);
  721. return 0;
  722. }
  723. #ifdef CONFIG_FS_DAX
  724. static int ext2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  725. unsigned flags, struct iomap *iomap)
  726. {
  727. unsigned int blkbits = inode->i_blkbits;
  728. unsigned long first_block = offset >> blkbits;
  729. unsigned long max_blocks = (length + (1 << blkbits) - 1) >> blkbits;
  730. bool new = false, boundary = false;
  731. u32 bno;
  732. int ret;
  733. ret = ext2_get_blocks(inode, first_block, max_blocks,
  734. &bno, &new, &boundary, flags & IOMAP_WRITE);
  735. if (ret < 0)
  736. return ret;
  737. iomap->flags = 0;
  738. iomap->bdev = inode->i_sb->s_bdev;
  739. iomap->offset = (u64)first_block << blkbits;
  740. if (ret == 0) {
  741. iomap->type = IOMAP_HOLE;
  742. iomap->blkno = IOMAP_NULL_BLOCK;
  743. iomap->length = 1 << blkbits;
  744. } else {
  745. iomap->type = IOMAP_MAPPED;
  746. iomap->blkno = (sector_t)bno << (blkbits - 9);
  747. iomap->length = (u64)ret << blkbits;
  748. iomap->flags |= IOMAP_F_MERGED;
  749. }
  750. if (new)
  751. iomap->flags |= IOMAP_F_NEW;
  752. return 0;
  753. }
  754. static int
  755. ext2_iomap_end(struct inode *inode, loff_t offset, loff_t length,
  756. ssize_t written, unsigned flags, struct iomap *iomap)
  757. {
  758. if (iomap->type == IOMAP_MAPPED &&
  759. written < length &&
  760. (flags & IOMAP_WRITE))
  761. ext2_write_failed(inode->i_mapping, offset + length);
  762. return 0;
  763. }
  764. struct iomap_ops ext2_iomap_ops = {
  765. .iomap_begin = ext2_iomap_begin,
  766. .iomap_end = ext2_iomap_end,
  767. };
  768. #endif /* CONFIG_FS_DAX */
  769. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  770. u64 start, u64 len)
  771. {
  772. return generic_block_fiemap(inode, fieinfo, start, len,
  773. ext2_get_block);
  774. }
  775. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  776. {
  777. return block_write_full_page(page, ext2_get_block, wbc);
  778. }
  779. static int ext2_readpage(struct file *file, struct page *page)
  780. {
  781. return mpage_readpage(page, ext2_get_block);
  782. }
  783. static int
  784. ext2_readpages(struct file *file, struct address_space *mapping,
  785. struct list_head *pages, unsigned nr_pages)
  786. {
  787. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  788. }
  789. static int
  790. ext2_write_begin(struct file *file, struct address_space *mapping,
  791. loff_t pos, unsigned len, unsigned flags,
  792. struct page **pagep, void **fsdata)
  793. {
  794. int ret;
  795. ret = block_write_begin(mapping, pos, len, flags, pagep,
  796. ext2_get_block);
  797. if (ret < 0)
  798. ext2_write_failed(mapping, pos + len);
  799. return ret;
  800. }
  801. static int ext2_write_end(struct file *file, struct address_space *mapping,
  802. loff_t pos, unsigned len, unsigned copied,
  803. struct page *page, void *fsdata)
  804. {
  805. int ret;
  806. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  807. if (ret < len)
  808. ext2_write_failed(mapping, pos + len);
  809. return ret;
  810. }
  811. static int
  812. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  813. loff_t pos, unsigned len, unsigned flags,
  814. struct page **pagep, void **fsdata)
  815. {
  816. int ret;
  817. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  818. ext2_get_block);
  819. if (ret < 0)
  820. ext2_write_failed(mapping, pos + len);
  821. return ret;
  822. }
  823. static int ext2_nobh_writepage(struct page *page,
  824. struct writeback_control *wbc)
  825. {
  826. return nobh_writepage(page, ext2_get_block, wbc);
  827. }
  828. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  829. {
  830. return generic_block_bmap(mapping,block,ext2_get_block);
  831. }
  832. static ssize_t
  833. ext2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  834. {
  835. struct file *file = iocb->ki_filp;
  836. struct address_space *mapping = file->f_mapping;
  837. struct inode *inode = mapping->host;
  838. size_t count = iov_iter_count(iter);
  839. loff_t offset = iocb->ki_pos;
  840. ssize_t ret;
  841. if (WARN_ON_ONCE(IS_DAX(inode)))
  842. return -EIO;
  843. ret = blockdev_direct_IO(iocb, inode, iter, ext2_get_block);
  844. if (ret < 0 && iov_iter_rw(iter) == WRITE)
  845. ext2_write_failed(mapping, offset + count);
  846. return ret;
  847. }
  848. static int
  849. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  850. {
  851. #ifdef CONFIG_FS_DAX
  852. if (dax_mapping(mapping)) {
  853. return dax_writeback_mapping_range(mapping,
  854. mapping->host->i_sb->s_bdev,
  855. wbc);
  856. }
  857. #endif
  858. return mpage_writepages(mapping, wbc, ext2_get_block);
  859. }
  860. const struct address_space_operations ext2_aops = {
  861. .readpage = ext2_readpage,
  862. .readpages = ext2_readpages,
  863. .writepage = ext2_writepage,
  864. .write_begin = ext2_write_begin,
  865. .write_end = ext2_write_end,
  866. .bmap = ext2_bmap,
  867. .direct_IO = ext2_direct_IO,
  868. .writepages = ext2_writepages,
  869. .migratepage = buffer_migrate_page,
  870. .is_partially_uptodate = block_is_partially_uptodate,
  871. .error_remove_page = generic_error_remove_page,
  872. };
  873. const struct address_space_operations ext2_nobh_aops = {
  874. .readpage = ext2_readpage,
  875. .readpages = ext2_readpages,
  876. .writepage = ext2_nobh_writepage,
  877. .write_begin = ext2_nobh_write_begin,
  878. .write_end = nobh_write_end,
  879. .bmap = ext2_bmap,
  880. .direct_IO = ext2_direct_IO,
  881. .writepages = ext2_writepages,
  882. .migratepage = buffer_migrate_page,
  883. .error_remove_page = generic_error_remove_page,
  884. };
  885. /*
  886. * Probably it should be a library function... search for first non-zero word
  887. * or memcmp with zero_page, whatever is better for particular architecture.
  888. * Linus?
  889. */
  890. static inline int all_zeroes(__le32 *p, __le32 *q)
  891. {
  892. while (p < q)
  893. if (*p++)
  894. return 0;
  895. return 1;
  896. }
  897. /**
  898. * ext2_find_shared - find the indirect blocks for partial truncation.
  899. * @inode: inode in question
  900. * @depth: depth of the affected branch
  901. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  902. * @chain: place to store the pointers to partial indirect blocks
  903. * @top: place to the (detached) top of branch
  904. *
  905. * This is a helper function used by ext2_truncate().
  906. *
  907. * When we do truncate() we may have to clean the ends of several indirect
  908. * blocks but leave the blocks themselves alive. Block is partially
  909. * truncated if some data below the new i_size is referred from it (and
  910. * it is on the path to the first completely truncated data block, indeed).
  911. * We have to free the top of that path along with everything to the right
  912. * of the path. Since no allocation past the truncation point is possible
  913. * until ext2_truncate() finishes, we may safely do the latter, but top
  914. * of branch may require special attention - pageout below the truncation
  915. * point might try to populate it.
  916. *
  917. * We atomically detach the top of branch from the tree, store the block
  918. * number of its root in *@top, pointers to buffer_heads of partially
  919. * truncated blocks - in @chain[].bh and pointers to their last elements
  920. * that should not be removed - in @chain[].p. Return value is the pointer
  921. * to last filled element of @chain.
  922. *
  923. * The work left to caller to do the actual freeing of subtrees:
  924. * a) free the subtree starting from *@top
  925. * b) free the subtrees whose roots are stored in
  926. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  927. * c) free the subtrees growing from the inode past the @chain[0].p
  928. * (no partially truncated stuff there).
  929. */
  930. static Indirect *ext2_find_shared(struct inode *inode,
  931. int depth,
  932. int offsets[4],
  933. Indirect chain[4],
  934. __le32 *top)
  935. {
  936. Indirect *partial, *p;
  937. int k, err;
  938. *top = 0;
  939. for (k = depth; k > 1 && !offsets[k-1]; k--)
  940. ;
  941. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  942. if (!partial)
  943. partial = chain + k-1;
  944. /*
  945. * If the branch acquired continuation since we've looked at it -
  946. * fine, it should all survive and (new) top doesn't belong to us.
  947. */
  948. write_lock(&EXT2_I(inode)->i_meta_lock);
  949. if (!partial->key && *partial->p) {
  950. write_unlock(&EXT2_I(inode)->i_meta_lock);
  951. goto no_top;
  952. }
  953. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  954. ;
  955. /*
  956. * OK, we've found the last block that must survive. The rest of our
  957. * branch should be detached before unlocking. However, if that rest
  958. * of branch is all ours and does not grow immediately from the inode
  959. * it's easier to cheat and just decrement partial->p.
  960. */
  961. if (p == chain + k - 1 && p > chain) {
  962. p->p--;
  963. } else {
  964. *top = *p->p;
  965. *p->p = 0;
  966. }
  967. write_unlock(&EXT2_I(inode)->i_meta_lock);
  968. while(partial > p)
  969. {
  970. brelse(partial->bh);
  971. partial--;
  972. }
  973. no_top:
  974. return partial;
  975. }
  976. /**
  977. * ext2_free_data - free a list of data blocks
  978. * @inode: inode we are dealing with
  979. * @p: array of block numbers
  980. * @q: points immediately past the end of array
  981. *
  982. * We are freeing all blocks referred from that array (numbers are
  983. * stored as little-endian 32-bit) and updating @inode->i_blocks
  984. * appropriately.
  985. */
  986. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  987. {
  988. unsigned long block_to_free = 0, count = 0;
  989. unsigned long nr;
  990. for ( ; p < q ; p++) {
  991. nr = le32_to_cpu(*p);
  992. if (nr) {
  993. *p = 0;
  994. /* accumulate blocks to free if they're contiguous */
  995. if (count == 0)
  996. goto free_this;
  997. else if (block_to_free == nr - count)
  998. count++;
  999. else {
  1000. ext2_free_blocks (inode, block_to_free, count);
  1001. mark_inode_dirty(inode);
  1002. free_this:
  1003. block_to_free = nr;
  1004. count = 1;
  1005. }
  1006. }
  1007. }
  1008. if (count > 0) {
  1009. ext2_free_blocks (inode, block_to_free, count);
  1010. mark_inode_dirty(inode);
  1011. }
  1012. }
  1013. /**
  1014. * ext2_free_branches - free an array of branches
  1015. * @inode: inode we are dealing with
  1016. * @p: array of block numbers
  1017. * @q: pointer immediately past the end of array
  1018. * @depth: depth of the branches to free
  1019. *
  1020. * We are freeing all blocks referred from these branches (numbers are
  1021. * stored as little-endian 32-bit) and updating @inode->i_blocks
  1022. * appropriately.
  1023. */
  1024. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  1025. {
  1026. struct buffer_head * bh;
  1027. unsigned long nr;
  1028. if (depth--) {
  1029. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1030. for ( ; p < q ; p++) {
  1031. nr = le32_to_cpu(*p);
  1032. if (!nr)
  1033. continue;
  1034. *p = 0;
  1035. bh = sb_bread(inode->i_sb, nr);
  1036. /*
  1037. * A read failure? Report error and clear slot
  1038. * (should be rare).
  1039. */
  1040. if (!bh) {
  1041. ext2_error(inode->i_sb, "ext2_free_branches",
  1042. "Read failure, inode=%ld, block=%ld",
  1043. inode->i_ino, nr);
  1044. continue;
  1045. }
  1046. ext2_free_branches(inode,
  1047. (__le32*)bh->b_data,
  1048. (__le32*)bh->b_data + addr_per_block,
  1049. depth);
  1050. bforget(bh);
  1051. ext2_free_blocks(inode, nr, 1);
  1052. mark_inode_dirty(inode);
  1053. }
  1054. } else
  1055. ext2_free_data(inode, p, q);
  1056. }
  1057. /* dax_sem must be held when calling this function */
  1058. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1059. {
  1060. __le32 *i_data = EXT2_I(inode)->i_data;
  1061. struct ext2_inode_info *ei = EXT2_I(inode);
  1062. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1063. int offsets[4];
  1064. Indirect chain[4];
  1065. Indirect *partial;
  1066. __le32 nr = 0;
  1067. int n;
  1068. long iblock;
  1069. unsigned blocksize;
  1070. blocksize = inode->i_sb->s_blocksize;
  1071. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  1072. #ifdef CONFIG_FS_DAX
  1073. WARN_ON(!rwsem_is_locked(&ei->dax_sem));
  1074. #endif
  1075. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  1076. if (n == 0)
  1077. return;
  1078. /*
  1079. * From here we block out all ext2_get_block() callers who want to
  1080. * modify the block allocation tree.
  1081. */
  1082. mutex_lock(&ei->truncate_mutex);
  1083. if (n == 1) {
  1084. ext2_free_data(inode, i_data+offsets[0],
  1085. i_data + EXT2_NDIR_BLOCKS);
  1086. goto do_indirects;
  1087. }
  1088. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  1089. /* Kill the top of shared branch (already detached) */
  1090. if (nr) {
  1091. if (partial == chain)
  1092. mark_inode_dirty(inode);
  1093. else
  1094. mark_buffer_dirty_inode(partial->bh, inode);
  1095. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1096. }
  1097. /* Clear the ends of indirect blocks on the shared branch */
  1098. while (partial > chain) {
  1099. ext2_free_branches(inode,
  1100. partial->p + 1,
  1101. (__le32*)partial->bh->b_data+addr_per_block,
  1102. (chain+n-1) - partial);
  1103. mark_buffer_dirty_inode(partial->bh, inode);
  1104. brelse (partial->bh);
  1105. partial--;
  1106. }
  1107. do_indirects:
  1108. /* Kill the remaining (whole) subtrees */
  1109. switch (offsets[0]) {
  1110. default:
  1111. nr = i_data[EXT2_IND_BLOCK];
  1112. if (nr) {
  1113. i_data[EXT2_IND_BLOCK] = 0;
  1114. mark_inode_dirty(inode);
  1115. ext2_free_branches(inode, &nr, &nr+1, 1);
  1116. }
  1117. case EXT2_IND_BLOCK:
  1118. nr = i_data[EXT2_DIND_BLOCK];
  1119. if (nr) {
  1120. i_data[EXT2_DIND_BLOCK] = 0;
  1121. mark_inode_dirty(inode);
  1122. ext2_free_branches(inode, &nr, &nr+1, 2);
  1123. }
  1124. case EXT2_DIND_BLOCK:
  1125. nr = i_data[EXT2_TIND_BLOCK];
  1126. if (nr) {
  1127. i_data[EXT2_TIND_BLOCK] = 0;
  1128. mark_inode_dirty(inode);
  1129. ext2_free_branches(inode, &nr, &nr+1, 3);
  1130. }
  1131. case EXT2_TIND_BLOCK:
  1132. ;
  1133. }
  1134. ext2_discard_reservation(inode);
  1135. mutex_unlock(&ei->truncate_mutex);
  1136. }
  1137. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1138. {
  1139. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1140. S_ISLNK(inode->i_mode)))
  1141. return;
  1142. if (ext2_inode_is_fast_symlink(inode))
  1143. return;
  1144. dax_sem_down_write(EXT2_I(inode));
  1145. __ext2_truncate_blocks(inode, offset);
  1146. dax_sem_up_write(EXT2_I(inode));
  1147. }
  1148. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1149. {
  1150. int error;
  1151. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1152. S_ISLNK(inode->i_mode)))
  1153. return -EINVAL;
  1154. if (ext2_inode_is_fast_symlink(inode))
  1155. return -EINVAL;
  1156. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1157. return -EPERM;
  1158. inode_dio_wait(inode);
  1159. if (IS_DAX(inode))
  1160. error = dax_truncate_page(inode, newsize, ext2_get_block);
  1161. else if (test_opt(inode->i_sb, NOBH))
  1162. error = nobh_truncate_page(inode->i_mapping,
  1163. newsize, ext2_get_block);
  1164. else
  1165. error = block_truncate_page(inode->i_mapping,
  1166. newsize, ext2_get_block);
  1167. if (error)
  1168. return error;
  1169. dax_sem_down_write(EXT2_I(inode));
  1170. truncate_setsize(inode, newsize);
  1171. __ext2_truncate_blocks(inode, newsize);
  1172. dax_sem_up_write(EXT2_I(inode));
  1173. inode->i_mtime = inode->i_ctime = current_time(inode);
  1174. if (inode_needs_sync(inode)) {
  1175. sync_mapping_buffers(inode->i_mapping);
  1176. sync_inode_metadata(inode, 1);
  1177. } else {
  1178. mark_inode_dirty(inode);
  1179. }
  1180. return 0;
  1181. }
  1182. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1183. struct buffer_head **p)
  1184. {
  1185. struct buffer_head * bh;
  1186. unsigned long block_group;
  1187. unsigned long block;
  1188. unsigned long offset;
  1189. struct ext2_group_desc * gdp;
  1190. *p = NULL;
  1191. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1192. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1193. goto Einval;
  1194. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1195. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1196. if (!gdp)
  1197. goto Egdp;
  1198. /*
  1199. * Figure out the offset within the block group inode table
  1200. */
  1201. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1202. block = le32_to_cpu(gdp->bg_inode_table) +
  1203. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1204. if (!(bh = sb_bread(sb, block)))
  1205. goto Eio;
  1206. *p = bh;
  1207. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1208. return (struct ext2_inode *) (bh->b_data + offset);
  1209. Einval:
  1210. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1211. (unsigned long) ino);
  1212. return ERR_PTR(-EINVAL);
  1213. Eio:
  1214. ext2_error(sb, "ext2_get_inode",
  1215. "unable to read inode block - inode=%lu, block=%lu",
  1216. (unsigned long) ino, block);
  1217. Egdp:
  1218. return ERR_PTR(-EIO);
  1219. }
  1220. void ext2_set_inode_flags(struct inode *inode)
  1221. {
  1222. unsigned int flags = EXT2_I(inode)->i_flags;
  1223. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  1224. S_DIRSYNC | S_DAX);
  1225. if (flags & EXT2_SYNC_FL)
  1226. inode->i_flags |= S_SYNC;
  1227. if (flags & EXT2_APPEND_FL)
  1228. inode->i_flags |= S_APPEND;
  1229. if (flags & EXT2_IMMUTABLE_FL)
  1230. inode->i_flags |= S_IMMUTABLE;
  1231. if (flags & EXT2_NOATIME_FL)
  1232. inode->i_flags |= S_NOATIME;
  1233. if (flags & EXT2_DIRSYNC_FL)
  1234. inode->i_flags |= S_DIRSYNC;
  1235. if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
  1236. inode->i_flags |= S_DAX;
  1237. }
  1238. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1239. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1240. {
  1241. unsigned int flags = ei->vfs_inode.i_flags;
  1242. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1243. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1244. if (flags & S_SYNC)
  1245. ei->i_flags |= EXT2_SYNC_FL;
  1246. if (flags & S_APPEND)
  1247. ei->i_flags |= EXT2_APPEND_FL;
  1248. if (flags & S_IMMUTABLE)
  1249. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1250. if (flags & S_NOATIME)
  1251. ei->i_flags |= EXT2_NOATIME_FL;
  1252. if (flags & S_DIRSYNC)
  1253. ei->i_flags |= EXT2_DIRSYNC_FL;
  1254. }
  1255. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1256. {
  1257. struct ext2_inode_info *ei;
  1258. struct buffer_head * bh;
  1259. struct ext2_inode *raw_inode;
  1260. struct inode *inode;
  1261. long ret = -EIO;
  1262. int n;
  1263. uid_t i_uid;
  1264. gid_t i_gid;
  1265. inode = iget_locked(sb, ino);
  1266. if (!inode)
  1267. return ERR_PTR(-ENOMEM);
  1268. if (!(inode->i_state & I_NEW))
  1269. return inode;
  1270. ei = EXT2_I(inode);
  1271. ei->i_block_alloc_info = NULL;
  1272. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1273. if (IS_ERR(raw_inode)) {
  1274. ret = PTR_ERR(raw_inode);
  1275. goto bad_inode;
  1276. }
  1277. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1278. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1279. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1280. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1281. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1282. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1283. }
  1284. i_uid_write(inode, i_uid);
  1285. i_gid_write(inode, i_gid);
  1286. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1287. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1288. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1289. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1290. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1291. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1292. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1293. /* We now have enough fields to check if the inode was active or not.
  1294. * This is needed because nfsd might try to access dead inodes
  1295. * the test is that same one that e2fsck uses
  1296. * NeilBrown 1999oct15
  1297. */
  1298. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1299. /* this inode is deleted */
  1300. brelse (bh);
  1301. ret = -ESTALE;
  1302. goto bad_inode;
  1303. }
  1304. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1305. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1306. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1307. ei->i_frag_no = raw_inode->i_frag;
  1308. ei->i_frag_size = raw_inode->i_fsize;
  1309. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1310. ei->i_dir_acl = 0;
  1311. if (ei->i_file_acl &&
  1312. !ext2_data_block_valid(EXT2_SB(sb), ei->i_file_acl, 1)) {
  1313. ext2_error(sb, "ext2_iget", "bad extended attribute block %u",
  1314. ei->i_file_acl);
  1315. brelse(bh);
  1316. ret = -EFSCORRUPTED;
  1317. goto bad_inode;
  1318. }
  1319. if (S_ISREG(inode->i_mode))
  1320. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1321. else
  1322. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1323. ei->i_dtime = 0;
  1324. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1325. ei->i_state = 0;
  1326. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1327. ei->i_dir_start_lookup = 0;
  1328. /*
  1329. * NOTE! The in-memory inode i_data array is in little-endian order
  1330. * even on big-endian machines: we do NOT byteswap the block numbers!
  1331. */
  1332. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1333. ei->i_data[n] = raw_inode->i_block[n];
  1334. if (S_ISREG(inode->i_mode)) {
  1335. inode->i_op = &ext2_file_inode_operations;
  1336. if (test_opt(inode->i_sb, NOBH)) {
  1337. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1338. inode->i_fop = &ext2_file_operations;
  1339. } else {
  1340. inode->i_mapping->a_ops = &ext2_aops;
  1341. inode->i_fop = &ext2_file_operations;
  1342. }
  1343. } else if (S_ISDIR(inode->i_mode)) {
  1344. inode->i_op = &ext2_dir_inode_operations;
  1345. inode->i_fop = &ext2_dir_operations;
  1346. if (test_opt(inode->i_sb, NOBH))
  1347. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1348. else
  1349. inode->i_mapping->a_ops = &ext2_aops;
  1350. } else if (S_ISLNK(inode->i_mode)) {
  1351. if (ext2_inode_is_fast_symlink(inode)) {
  1352. inode->i_link = (char *)ei->i_data;
  1353. inode->i_op = &ext2_fast_symlink_inode_operations;
  1354. nd_terminate_link(ei->i_data, inode->i_size,
  1355. sizeof(ei->i_data) - 1);
  1356. } else {
  1357. inode->i_op = &ext2_symlink_inode_operations;
  1358. inode_nohighmem(inode);
  1359. if (test_opt(inode->i_sb, NOBH))
  1360. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1361. else
  1362. inode->i_mapping->a_ops = &ext2_aops;
  1363. }
  1364. } else {
  1365. inode->i_op = &ext2_special_inode_operations;
  1366. if (raw_inode->i_block[0])
  1367. init_special_inode(inode, inode->i_mode,
  1368. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1369. else
  1370. init_special_inode(inode, inode->i_mode,
  1371. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1372. }
  1373. brelse (bh);
  1374. ext2_set_inode_flags(inode);
  1375. unlock_new_inode(inode);
  1376. return inode;
  1377. bad_inode:
  1378. iget_failed(inode);
  1379. return ERR_PTR(ret);
  1380. }
  1381. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1382. {
  1383. struct ext2_inode_info *ei = EXT2_I(inode);
  1384. struct super_block *sb = inode->i_sb;
  1385. ino_t ino = inode->i_ino;
  1386. uid_t uid = i_uid_read(inode);
  1387. gid_t gid = i_gid_read(inode);
  1388. struct buffer_head * bh;
  1389. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1390. int n;
  1391. int err = 0;
  1392. if (IS_ERR(raw_inode))
  1393. return -EIO;
  1394. /* For fields not not tracking in the in-memory inode,
  1395. * initialise them to zero for new inodes. */
  1396. if (ei->i_state & EXT2_STATE_NEW)
  1397. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1398. ext2_get_inode_flags(ei);
  1399. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1400. if (!(test_opt(sb, NO_UID32))) {
  1401. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1402. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1403. /*
  1404. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1405. * re-used with the upper 16 bits of the uid/gid intact
  1406. */
  1407. if (!ei->i_dtime) {
  1408. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1409. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1410. } else {
  1411. raw_inode->i_uid_high = 0;
  1412. raw_inode->i_gid_high = 0;
  1413. }
  1414. } else {
  1415. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1416. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1417. raw_inode->i_uid_high = 0;
  1418. raw_inode->i_gid_high = 0;
  1419. }
  1420. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1421. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1422. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1423. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1424. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1425. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1426. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1427. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1428. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1429. raw_inode->i_frag = ei->i_frag_no;
  1430. raw_inode->i_fsize = ei->i_frag_size;
  1431. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1432. if (!S_ISREG(inode->i_mode))
  1433. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1434. else {
  1435. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1436. if (inode->i_size > 0x7fffffffULL) {
  1437. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1438. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1439. EXT2_SB(sb)->s_es->s_rev_level ==
  1440. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1441. /* If this is the first large file
  1442. * created, add a flag to the superblock.
  1443. */
  1444. spin_lock(&EXT2_SB(sb)->s_lock);
  1445. ext2_update_dynamic_rev(sb);
  1446. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1447. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1448. spin_unlock(&EXT2_SB(sb)->s_lock);
  1449. ext2_write_super(sb);
  1450. }
  1451. }
  1452. }
  1453. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1454. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1455. if (old_valid_dev(inode->i_rdev)) {
  1456. raw_inode->i_block[0] =
  1457. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1458. raw_inode->i_block[1] = 0;
  1459. } else {
  1460. raw_inode->i_block[0] = 0;
  1461. raw_inode->i_block[1] =
  1462. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1463. raw_inode->i_block[2] = 0;
  1464. }
  1465. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1466. raw_inode->i_block[n] = ei->i_data[n];
  1467. mark_buffer_dirty(bh);
  1468. if (do_sync) {
  1469. sync_dirty_buffer(bh);
  1470. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1471. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1472. sb->s_id, (unsigned long) ino);
  1473. err = -EIO;
  1474. }
  1475. }
  1476. ei->i_state &= ~EXT2_STATE_NEW;
  1477. brelse (bh);
  1478. return err;
  1479. }
  1480. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1481. {
  1482. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1483. }
  1484. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1485. {
  1486. struct inode *inode = d_inode(dentry);
  1487. int error;
  1488. error = setattr_prepare(dentry, iattr);
  1489. if (error)
  1490. return error;
  1491. if (is_quota_modification(inode, iattr)) {
  1492. error = dquot_initialize(inode);
  1493. if (error)
  1494. return error;
  1495. }
  1496. if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  1497. (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
  1498. error = dquot_transfer(inode, iattr);
  1499. if (error)
  1500. return error;
  1501. }
  1502. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1503. error = ext2_setsize(inode, iattr->ia_size);
  1504. if (error)
  1505. return error;
  1506. }
  1507. setattr_copy(inode, iattr);
  1508. if (iattr->ia_valid & ATTR_MODE)
  1509. error = posix_acl_chmod(inode, inode->i_mode);
  1510. mark_inode_dirty(inode);
  1511. return error;
  1512. }