inode.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * linux/fs/ufs/inode.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * from
  9. *
  10. * linux/fs/ext2/inode.c
  11. *
  12. * Copyright (C) 1992, 1993, 1994, 1995
  13. * Remy Card (card@masi.ibp.fr)
  14. * Laboratoire MASI - Institut Blaise Pascal
  15. * Universite Pierre et Marie Curie (Paris VI)
  16. *
  17. * from
  18. *
  19. * linux/fs/minix/inode.c
  20. *
  21. * Copyright (C) 1991, 1992 Linus Torvalds
  22. *
  23. * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  24. * Big-endian to little-endian byte-swapping/bitmaps by
  25. * David S. Miller (davem@caip.rutgers.edu), 1995
  26. */
  27. #include <asm/uaccess.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/time.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/mm.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/writeback.h>
  36. #include "ufs_fs.h"
  37. #include "ufs.h"
  38. #include "swab.h"
  39. #include "util.h"
  40. static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
  41. {
  42. struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
  43. int ptrs = uspi->s_apb;
  44. int ptrs_bits = uspi->s_apbshift;
  45. const long direct_blocks = UFS_NDADDR,
  46. indirect_blocks = ptrs,
  47. double_blocks = (1 << (ptrs_bits * 2));
  48. int n = 0;
  49. UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
  50. if (i_block < direct_blocks) {
  51. offsets[n++] = i_block;
  52. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  53. offsets[n++] = UFS_IND_BLOCK;
  54. offsets[n++] = i_block;
  55. } else if ((i_block -= indirect_blocks) < double_blocks) {
  56. offsets[n++] = UFS_DIND_BLOCK;
  57. offsets[n++] = i_block >> ptrs_bits;
  58. offsets[n++] = i_block & (ptrs - 1);
  59. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  60. offsets[n++] = UFS_TIND_BLOCK;
  61. offsets[n++] = i_block >> (ptrs_bits * 2);
  62. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  63. offsets[n++] = i_block & (ptrs - 1);
  64. } else {
  65. ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
  66. }
  67. return n;
  68. }
  69. typedef struct {
  70. void *p;
  71. union {
  72. __fs32 key32;
  73. __fs64 key64;
  74. };
  75. struct buffer_head *bh;
  76. } Indirect;
  77. static inline int grow_chain32(struct ufs_inode_info *ufsi,
  78. struct buffer_head *bh, __fs32 *v,
  79. Indirect *from, Indirect *to)
  80. {
  81. Indirect *p;
  82. unsigned seq;
  83. to->bh = bh;
  84. do {
  85. seq = read_seqbegin(&ufsi->meta_lock);
  86. to->key32 = *(__fs32 *)(to->p = v);
  87. for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++)
  88. ;
  89. } while (read_seqretry(&ufsi->meta_lock, seq));
  90. return (p > to);
  91. }
  92. static inline int grow_chain64(struct ufs_inode_info *ufsi,
  93. struct buffer_head *bh, __fs64 *v,
  94. Indirect *from, Indirect *to)
  95. {
  96. Indirect *p;
  97. unsigned seq;
  98. to->bh = bh;
  99. do {
  100. seq = read_seqbegin(&ufsi->meta_lock);
  101. to->key64 = *(__fs64 *)(to->p = v);
  102. for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++)
  103. ;
  104. } while (read_seqretry(&ufsi->meta_lock, seq));
  105. return (p > to);
  106. }
  107. /*
  108. * Returns the location of the fragment from
  109. * the beginning of the filesystem.
  110. */
  111. static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth)
  112. {
  113. struct ufs_inode_info *ufsi = UFS_I(inode);
  114. struct super_block *sb = inode->i_sb;
  115. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  116. u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
  117. int shift = uspi->s_apbshift-uspi->s_fpbshift;
  118. Indirect chain[4], *q = chain;
  119. unsigned *p;
  120. unsigned flags = UFS_SB(sb)->s_flags;
  121. u64 res = 0;
  122. UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
  123. uspi->s_fpbshift, uspi->s_apbmask,
  124. (unsigned long long)mask);
  125. if (depth == 0)
  126. goto no_block;
  127. again:
  128. p = offsets;
  129. if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
  130. goto ufs2;
  131. if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
  132. goto changed;
  133. if (!q->key32)
  134. goto no_block;
  135. while (--depth) {
  136. __fs32 *ptr;
  137. struct buffer_head *bh;
  138. unsigned n = *p++;
  139. bh = sb_bread(sb, uspi->s_sbbase +
  140. fs32_to_cpu(sb, q->key32) + (n>>shift));
  141. if (!bh)
  142. goto no_block;
  143. ptr = (__fs32 *)bh->b_data + (n & mask);
  144. if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
  145. goto changed;
  146. if (!q->key32)
  147. goto no_block;
  148. }
  149. res = fs32_to_cpu(sb, q->key32);
  150. goto found;
  151. ufs2:
  152. if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
  153. goto changed;
  154. if (!q->key64)
  155. goto no_block;
  156. while (--depth) {
  157. __fs64 *ptr;
  158. struct buffer_head *bh;
  159. unsigned n = *p++;
  160. bh = sb_bread(sb, uspi->s_sbbase +
  161. fs64_to_cpu(sb, q->key64) + (n>>shift));
  162. if (!bh)
  163. goto no_block;
  164. ptr = (__fs64 *)bh->b_data + (n & mask);
  165. if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
  166. goto changed;
  167. if (!q->key64)
  168. goto no_block;
  169. }
  170. res = fs64_to_cpu(sb, q->key64);
  171. found:
  172. res += uspi->s_sbbase;
  173. no_block:
  174. while (q > chain) {
  175. brelse(q->bh);
  176. q--;
  177. }
  178. return res;
  179. changed:
  180. while (q > chain) {
  181. brelse(q->bh);
  182. q--;
  183. }
  184. goto again;
  185. }
  186. /*
  187. * Unpacking tails: we have a file with partial final block and
  188. * we had been asked to extend it. If the fragment being written
  189. * is within the same block, we need to extend the tail just to cover
  190. * that fragment. Otherwise the tail is extended to full block.
  191. *
  192. * Note that we might need to create a _new_ tail, but that will
  193. * be handled elsewhere; this is strictly for resizing old
  194. * ones.
  195. */
  196. static bool
  197. ufs_extend_tail(struct inode *inode, u64 writes_to,
  198. int *err, struct page *locked_page)
  199. {
  200. struct ufs_inode_info *ufsi = UFS_I(inode);
  201. struct super_block *sb = inode->i_sb;
  202. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  203. unsigned lastfrag = ufsi->i_lastfrag; /* it's a short file, so unsigned is enough */
  204. unsigned block = ufs_fragstoblks(lastfrag);
  205. unsigned new_size;
  206. void *p;
  207. u64 tmp;
  208. if (writes_to < (lastfrag | uspi->s_fpbmask))
  209. new_size = (writes_to & uspi->s_fpbmask) + 1;
  210. else
  211. new_size = uspi->s_fpb;
  212. p = ufs_get_direct_data_ptr(uspi, ufsi, block);
  213. tmp = ufs_new_fragments(inode, p, lastfrag, ufs_data_ptr_to_cpu(sb, p),
  214. new_size - (lastfrag & uspi->s_fpbmask), err,
  215. locked_page);
  216. return tmp != 0;
  217. }
  218. /**
  219. * ufs_inode_getfrag() - allocate new fragment(s)
  220. * @inode: pointer to inode
  221. * @index: number of block pointer within the inode's array.
  222. * @new_fragment: number of new allocated fragment(s)
  223. * @err: we set it if something wrong
  224. * @new: we set it if we allocate new block
  225. * @locked_page: for ufs_new_fragments()
  226. */
  227. static u64
  228. ufs_inode_getfrag(struct inode *inode, unsigned index,
  229. sector_t new_fragment, int *err,
  230. int *new, struct page *locked_page)
  231. {
  232. struct ufs_inode_info *ufsi = UFS_I(inode);
  233. struct super_block *sb = inode->i_sb;
  234. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  235. u64 tmp, goal, lastfrag;
  236. unsigned nfrags = uspi->s_fpb;
  237. void *p;
  238. /* TODO : to be done for write support
  239. if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
  240. goto ufs2;
  241. */
  242. p = ufs_get_direct_data_ptr(uspi, ufsi, index);
  243. tmp = ufs_data_ptr_to_cpu(sb, p);
  244. if (tmp)
  245. goto out;
  246. lastfrag = ufsi->i_lastfrag;
  247. /* will that be a new tail? */
  248. if (new_fragment < UFS_NDIR_FRAGMENT && new_fragment >= lastfrag)
  249. nfrags = (new_fragment & uspi->s_fpbmask) + 1;
  250. goal = 0;
  251. if (index) {
  252. goal = ufs_data_ptr_to_cpu(sb,
  253. ufs_get_direct_data_ptr(uspi, ufsi, index - 1));
  254. if (goal)
  255. goal += uspi->s_fpb;
  256. }
  257. tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment),
  258. goal, nfrags, err, locked_page);
  259. if (!tmp) {
  260. *err = -ENOSPC;
  261. return 0;
  262. }
  263. if (new)
  264. *new = 1;
  265. inode->i_ctime = CURRENT_TIME_SEC;
  266. if (IS_SYNC(inode))
  267. ufs_sync_inode (inode);
  268. mark_inode_dirty(inode);
  269. out:
  270. return tmp + uspi->s_sbbase;
  271. /* This part : To be implemented ....
  272. Required only for writing, not required for READ-ONLY.
  273. ufs2:
  274. u2_block = ufs_fragstoblks(fragment);
  275. u2_blockoff = ufs_fragnum(fragment);
  276. p = ufsi->i_u1.u2_i_data + block;
  277. goal = 0;
  278. repeat2:
  279. tmp = fs32_to_cpu(sb, *p);
  280. lastfrag = ufsi->i_lastfrag;
  281. */
  282. }
  283. /**
  284. * ufs_inode_getblock() - allocate new block
  285. * @inode: pointer to inode
  286. * @ind_block: block number of the indirect block
  287. * @index: number of pointer within the indirect block
  288. * @new_fragment: number of new allocated fragment
  289. * (block will hold this fragment and also uspi->s_fpb-1)
  290. * @err: see ufs_inode_getfrag()
  291. * @new: see ufs_inode_getfrag()
  292. * @locked_page: see ufs_inode_getfrag()
  293. */
  294. static u64
  295. ufs_inode_getblock(struct inode *inode, u64 ind_block,
  296. unsigned index, sector_t new_fragment, int *err,
  297. int *new, struct page *locked_page)
  298. {
  299. struct super_block *sb = inode->i_sb;
  300. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  301. int shift = uspi->s_apbshift - uspi->s_fpbshift;
  302. u64 tmp = 0, goal;
  303. struct buffer_head *bh;
  304. void *p;
  305. if (!ind_block)
  306. return 0;
  307. bh = sb_bread(sb, ind_block + (index >> shift));
  308. if (unlikely(!bh)) {
  309. *err = -EIO;
  310. return 0;
  311. }
  312. index &= uspi->s_apbmask >> uspi->s_fpbshift;
  313. if (uspi->fs_magic == UFS2_MAGIC)
  314. p = (__fs64 *)bh->b_data + index;
  315. else
  316. p = (__fs32 *)bh->b_data + index;
  317. tmp = ufs_data_ptr_to_cpu(sb, p);
  318. if (tmp)
  319. goto out;
  320. if (index && (uspi->fs_magic == UFS2_MAGIC ?
  321. (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[index-1])) :
  322. (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[index-1]))))
  323. goal = tmp + uspi->s_fpb;
  324. else
  325. goal = bh->b_blocknr + uspi->s_fpb;
  326. tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
  327. uspi->s_fpb, err, locked_page);
  328. if (!tmp)
  329. goto out;
  330. if (new)
  331. *new = 1;
  332. mark_buffer_dirty(bh);
  333. if (IS_SYNC(inode))
  334. sync_dirty_buffer(bh);
  335. inode->i_ctime = CURRENT_TIME_SEC;
  336. mark_inode_dirty(inode);
  337. out:
  338. brelse (bh);
  339. UFSD("EXIT\n");
  340. if (tmp)
  341. tmp += uspi->s_sbbase;
  342. return tmp;
  343. }
  344. /**
  345. * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
  346. * readpage, writepage and so on
  347. */
  348. static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
  349. {
  350. struct super_block *sb = inode->i_sb;
  351. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  352. int err = 0, new = 0;
  353. unsigned offsets[4];
  354. int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets);
  355. u64 phys64 = 0;
  356. unsigned frag = fragment & uspi->s_fpbmask;
  357. if (!create) {
  358. phys64 = ufs_frag_map(inode, offsets, depth);
  359. if (phys64)
  360. map_bh(bh_result, sb, phys64 + frag);
  361. return 0;
  362. }
  363. /* This code entered only while writing ....? */
  364. mutex_lock(&UFS_I(inode)->truncate_mutex);
  365. UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
  366. if (unlikely(!depth)) {
  367. ufs_warning(sb, "ufs_get_block", "block > big");
  368. err = -EIO;
  369. goto out;
  370. }
  371. if (UFS_I(inode)->i_lastfrag < UFS_NDIR_FRAGMENT) {
  372. unsigned lastfrag = UFS_I(inode)->i_lastfrag;
  373. unsigned tailfrags = lastfrag & uspi->s_fpbmask;
  374. if (tailfrags && fragment >= lastfrag) {
  375. if (!ufs_extend_tail(inode, fragment,
  376. &err, bh_result->b_page))
  377. goto out;
  378. }
  379. }
  380. if (depth == 1) {
  381. phys64 = ufs_inode_getfrag(inode, offsets[0], fragment,
  382. &err, &new, bh_result->b_page);
  383. } else {
  384. int i;
  385. phys64 = ufs_inode_getfrag(inode, offsets[0], fragment,
  386. &err, NULL, NULL);
  387. for (i = 1; i < depth - 1; i++)
  388. phys64 = ufs_inode_getblock(inode, phys64, offsets[i],
  389. fragment, &err, NULL, NULL);
  390. phys64 = ufs_inode_getblock(inode, phys64, offsets[depth - 1],
  391. fragment, &err, &new, bh_result->b_page);
  392. }
  393. out:
  394. if (phys64) {
  395. phys64 += frag;
  396. map_bh(bh_result, sb, phys64);
  397. if (new)
  398. set_buffer_new(bh_result);
  399. }
  400. mutex_unlock(&UFS_I(inode)->truncate_mutex);
  401. return err;
  402. }
  403. static int ufs_writepage(struct page *page, struct writeback_control *wbc)
  404. {
  405. return block_write_full_page(page,ufs_getfrag_block,wbc);
  406. }
  407. static int ufs_readpage(struct file *file, struct page *page)
  408. {
  409. return block_read_full_page(page,ufs_getfrag_block);
  410. }
  411. int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
  412. {
  413. return __block_write_begin(page, pos, len, ufs_getfrag_block);
  414. }
  415. static void ufs_truncate_blocks(struct inode *);
  416. static void ufs_write_failed(struct address_space *mapping, loff_t to)
  417. {
  418. struct inode *inode = mapping->host;
  419. if (to > inode->i_size) {
  420. truncate_pagecache(inode, inode->i_size);
  421. ufs_truncate_blocks(inode);
  422. }
  423. }
  424. static int ufs_write_begin(struct file *file, struct address_space *mapping,
  425. loff_t pos, unsigned len, unsigned flags,
  426. struct page **pagep, void **fsdata)
  427. {
  428. int ret;
  429. ret = block_write_begin(mapping, pos, len, flags, pagep,
  430. ufs_getfrag_block);
  431. if (unlikely(ret))
  432. ufs_write_failed(mapping, pos + len);
  433. return ret;
  434. }
  435. static int ufs_write_end(struct file *file, struct address_space *mapping,
  436. loff_t pos, unsigned len, unsigned copied,
  437. struct page *page, void *fsdata)
  438. {
  439. int ret;
  440. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  441. if (ret < len)
  442. ufs_write_failed(mapping, pos + len);
  443. return ret;
  444. }
  445. static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
  446. {
  447. return generic_block_bmap(mapping,block,ufs_getfrag_block);
  448. }
  449. const struct address_space_operations ufs_aops = {
  450. .readpage = ufs_readpage,
  451. .writepage = ufs_writepage,
  452. .write_begin = ufs_write_begin,
  453. .write_end = ufs_write_end,
  454. .bmap = ufs_bmap
  455. };
  456. static void ufs_set_inode_ops(struct inode *inode)
  457. {
  458. if (S_ISREG(inode->i_mode)) {
  459. inode->i_op = &ufs_file_inode_operations;
  460. inode->i_fop = &ufs_file_operations;
  461. inode->i_mapping->a_ops = &ufs_aops;
  462. } else if (S_ISDIR(inode->i_mode)) {
  463. inode->i_op = &ufs_dir_inode_operations;
  464. inode->i_fop = &ufs_dir_operations;
  465. inode->i_mapping->a_ops = &ufs_aops;
  466. } else if (S_ISLNK(inode->i_mode)) {
  467. if (!inode->i_blocks) {
  468. inode->i_op = &ufs_fast_symlink_inode_operations;
  469. inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
  470. } else {
  471. inode->i_op = &ufs_symlink_inode_operations;
  472. inode->i_mapping->a_ops = &ufs_aops;
  473. }
  474. } else
  475. init_special_inode(inode, inode->i_mode,
  476. ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
  477. }
  478. static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  479. {
  480. struct ufs_inode_info *ufsi = UFS_I(inode);
  481. struct super_block *sb = inode->i_sb;
  482. umode_t mode;
  483. /*
  484. * Copy data to the in-core inode.
  485. */
  486. inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
  487. set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
  488. if (inode->i_nlink == 0) {
  489. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  490. return -1;
  491. }
  492. /*
  493. * Linux now has 32-bit uid and gid, so we can support EFT.
  494. */
  495. i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
  496. i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
  497. inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
  498. inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
  499. inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
  500. inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
  501. inode->i_mtime.tv_nsec = 0;
  502. inode->i_atime.tv_nsec = 0;
  503. inode->i_ctime.tv_nsec = 0;
  504. inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
  505. inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
  506. ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
  507. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  508. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  509. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  510. memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
  511. sizeof(ufs_inode->ui_u2.ui_addr));
  512. } else {
  513. memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
  514. sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
  515. ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
  516. }
  517. return 0;
  518. }
  519. static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
  520. {
  521. struct ufs_inode_info *ufsi = UFS_I(inode);
  522. struct super_block *sb = inode->i_sb;
  523. umode_t mode;
  524. UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
  525. /*
  526. * Copy data to the in-core inode.
  527. */
  528. inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
  529. set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
  530. if (inode->i_nlink == 0) {
  531. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  532. return -1;
  533. }
  534. /*
  535. * Linux now has 32-bit uid and gid, so we can support EFT.
  536. */
  537. i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
  538. i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
  539. inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
  540. inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
  541. inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
  542. inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
  543. inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
  544. inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
  545. inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
  546. inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
  547. inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
  548. ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
  549. /*
  550. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  551. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  552. */
  553. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  554. memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
  555. sizeof(ufs2_inode->ui_u2.ui_addr));
  556. } else {
  557. memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
  558. sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
  559. ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
  560. }
  561. return 0;
  562. }
  563. struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
  564. {
  565. struct ufs_inode_info *ufsi;
  566. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  567. struct buffer_head * bh;
  568. struct inode *inode;
  569. int err;
  570. UFSD("ENTER, ino %lu\n", ino);
  571. if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
  572. ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
  573. ino);
  574. return ERR_PTR(-EIO);
  575. }
  576. inode = iget_locked(sb, ino);
  577. if (!inode)
  578. return ERR_PTR(-ENOMEM);
  579. if (!(inode->i_state & I_NEW))
  580. return inode;
  581. ufsi = UFS_I(inode);
  582. bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
  583. if (!bh) {
  584. ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
  585. inode->i_ino);
  586. goto bad_inode;
  587. }
  588. if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
  589. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  590. err = ufs2_read_inode(inode,
  591. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  592. } else {
  593. struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
  594. err = ufs1_read_inode(inode,
  595. ufs_inode + ufs_inotofsbo(inode->i_ino));
  596. }
  597. if (err)
  598. goto bad_inode;
  599. inode->i_version++;
  600. ufsi->i_lastfrag =
  601. (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
  602. ufsi->i_dir_start_lookup = 0;
  603. ufsi->i_osync = 0;
  604. ufs_set_inode_ops(inode);
  605. brelse(bh);
  606. UFSD("EXIT\n");
  607. unlock_new_inode(inode);
  608. return inode;
  609. bad_inode:
  610. iget_failed(inode);
  611. return ERR_PTR(-EIO);
  612. }
  613. static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  614. {
  615. struct super_block *sb = inode->i_sb;
  616. struct ufs_inode_info *ufsi = UFS_I(inode);
  617. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  618. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  619. ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
  620. ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
  621. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  622. ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
  623. ufs_inode->ui_atime.tv_usec = 0;
  624. ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
  625. ufs_inode->ui_ctime.tv_usec = 0;
  626. ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
  627. ufs_inode->ui_mtime.tv_usec = 0;
  628. ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
  629. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  630. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  631. if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
  632. ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
  633. ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
  634. }
  635. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  636. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  637. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
  638. } else if (inode->i_blocks) {
  639. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
  640. sizeof(ufs_inode->ui_u2.ui_addr));
  641. }
  642. else {
  643. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  644. sizeof(ufs_inode->ui_u2.ui_symlink));
  645. }
  646. if (!inode->i_nlink)
  647. memset (ufs_inode, 0, sizeof(struct ufs_inode));
  648. }
  649. static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
  650. {
  651. struct super_block *sb = inode->i_sb;
  652. struct ufs_inode_info *ufsi = UFS_I(inode);
  653. UFSD("ENTER\n");
  654. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  655. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  656. ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
  657. ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
  658. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  659. ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
  660. ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
  661. ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
  662. ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
  663. ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
  664. ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
  665. ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
  666. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  667. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  668. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  669. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  670. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
  671. } else if (inode->i_blocks) {
  672. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
  673. sizeof(ufs_inode->ui_u2.ui_addr));
  674. } else {
  675. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  676. sizeof(ufs_inode->ui_u2.ui_symlink));
  677. }
  678. if (!inode->i_nlink)
  679. memset (ufs_inode, 0, sizeof(struct ufs2_inode));
  680. UFSD("EXIT\n");
  681. }
  682. static int ufs_update_inode(struct inode * inode, int do_sync)
  683. {
  684. struct super_block *sb = inode->i_sb;
  685. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  686. struct buffer_head * bh;
  687. UFSD("ENTER, ino %lu\n", inode->i_ino);
  688. if (inode->i_ino < UFS_ROOTINO ||
  689. inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
  690. ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
  691. return -1;
  692. }
  693. bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
  694. if (!bh) {
  695. ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
  696. return -1;
  697. }
  698. if (uspi->fs_magic == UFS2_MAGIC) {
  699. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  700. ufs2_update_inode(inode,
  701. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  702. } else {
  703. struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
  704. ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
  705. }
  706. mark_buffer_dirty(bh);
  707. if (do_sync)
  708. sync_dirty_buffer(bh);
  709. brelse (bh);
  710. UFSD("EXIT\n");
  711. return 0;
  712. }
  713. int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
  714. {
  715. return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  716. }
  717. int ufs_sync_inode (struct inode *inode)
  718. {
  719. return ufs_update_inode (inode, 1);
  720. }
  721. void ufs_evict_inode(struct inode * inode)
  722. {
  723. int want_delete = 0;
  724. if (!inode->i_nlink && !is_bad_inode(inode))
  725. want_delete = 1;
  726. truncate_inode_pages_final(&inode->i_data);
  727. if (want_delete) {
  728. inode->i_size = 0;
  729. if (inode->i_blocks)
  730. ufs_truncate_blocks(inode);
  731. }
  732. invalidate_inode_buffers(inode);
  733. clear_inode(inode);
  734. if (want_delete)
  735. ufs_free_inode(inode);
  736. }
  737. struct to_free {
  738. struct inode *inode;
  739. u64 to;
  740. unsigned count;
  741. };
  742. static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
  743. {
  744. if (ctx->count && ctx->to != from) {
  745. ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
  746. ctx->count = 0;
  747. }
  748. ctx->count += count;
  749. ctx->to = from + count;
  750. }
  751. #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
  752. #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
  753. static void ufs_trunc_direct(struct inode *inode)
  754. {
  755. struct ufs_inode_info *ufsi = UFS_I(inode);
  756. struct super_block * sb;
  757. struct ufs_sb_private_info * uspi;
  758. void *p;
  759. u64 frag1, frag2, frag3, frag4, block1, block2;
  760. struct to_free ctx = {.inode = inode};
  761. unsigned i, tmp;
  762. UFSD("ENTER: ino %lu\n", inode->i_ino);
  763. sb = inode->i_sb;
  764. uspi = UFS_SB(sb)->s_uspi;
  765. frag1 = DIRECT_FRAGMENT;
  766. frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
  767. frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
  768. frag3 = frag4 & ~uspi->s_fpbmask;
  769. block1 = block2 = 0;
  770. if (frag2 > frag3) {
  771. frag2 = frag4;
  772. frag3 = frag4 = 0;
  773. } else if (frag2 < frag3) {
  774. block1 = ufs_fragstoblks (frag2);
  775. block2 = ufs_fragstoblks (frag3);
  776. }
  777. UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
  778. " frag3 %llu, frag4 %llu\n", inode->i_ino,
  779. (unsigned long long)frag1, (unsigned long long)frag2,
  780. (unsigned long long)block1, (unsigned long long)block2,
  781. (unsigned long long)frag3, (unsigned long long)frag4);
  782. if (frag1 >= frag2)
  783. goto next1;
  784. /*
  785. * Free first free fragments
  786. */
  787. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
  788. tmp = ufs_data_ptr_to_cpu(sb, p);
  789. if (!tmp )
  790. ufs_panic (sb, "ufs_trunc_direct", "internal error");
  791. frag2 -= frag1;
  792. frag1 = ufs_fragnum (frag1);
  793. ufs_free_fragments(inode, tmp + frag1, frag2);
  794. next1:
  795. /*
  796. * Free whole blocks
  797. */
  798. for (i = block1 ; i < block2; i++) {
  799. p = ufs_get_direct_data_ptr(uspi, ufsi, i);
  800. tmp = ufs_data_ptr_to_cpu(sb, p);
  801. if (!tmp)
  802. continue;
  803. write_seqlock(&ufsi->meta_lock);
  804. ufs_data_ptr_clear(uspi, p);
  805. write_sequnlock(&ufsi->meta_lock);
  806. free_data(&ctx, tmp, uspi->s_fpb);
  807. }
  808. free_data(&ctx, 0, 0);
  809. if (frag3 >= frag4)
  810. goto next3;
  811. /*
  812. * Free last free fragments
  813. */
  814. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
  815. tmp = ufs_data_ptr_to_cpu(sb, p);
  816. if (!tmp )
  817. ufs_panic(sb, "ufs_truncate_direct", "internal error");
  818. frag4 = ufs_fragnum (frag4);
  819. write_seqlock(&ufsi->meta_lock);
  820. ufs_data_ptr_clear(uspi, p);
  821. write_sequnlock(&ufsi->meta_lock);
  822. ufs_free_fragments (inode, tmp, frag4);
  823. next3:
  824. UFSD("EXIT: ino %lu\n", inode->i_ino);
  825. }
  826. static void free_full_branch(struct inode *inode, u64 ind_block, int depth)
  827. {
  828. struct super_block *sb = inode->i_sb;
  829. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  830. struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize);
  831. unsigned i;
  832. if (!ubh)
  833. return;
  834. if (--depth) {
  835. for (i = 0; i < uspi->s_apb; i++) {
  836. void *p = ubh_get_data_ptr(uspi, ubh, i);
  837. u64 block = ufs_data_ptr_to_cpu(sb, p);
  838. if (block)
  839. free_full_branch(inode, block, depth);
  840. }
  841. } else {
  842. struct to_free ctx = {.inode = inode};
  843. for (i = 0; i < uspi->s_apb; i++) {
  844. void *p = ubh_get_data_ptr(uspi, ubh, i);
  845. u64 block = ufs_data_ptr_to_cpu(sb, p);
  846. if (block)
  847. free_data(&ctx, block, uspi->s_fpb);
  848. }
  849. free_data(&ctx, 0, 0);
  850. }
  851. ubh_bforget(ubh);
  852. ufs_free_blocks(inode, ind_block, uspi->s_fpb);
  853. }
  854. static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth)
  855. {
  856. struct super_block *sb = inode->i_sb;
  857. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  858. unsigned i;
  859. if (--depth) {
  860. for (i = from; i < uspi->s_apb ; i++) {
  861. void *p = ubh_get_data_ptr(uspi, ubh, i);
  862. u64 block = ufs_data_ptr_to_cpu(sb, p);
  863. if (block) {
  864. write_seqlock(&UFS_I(inode)->meta_lock);
  865. ufs_data_ptr_clear(uspi, p);
  866. write_sequnlock(&UFS_I(inode)->meta_lock);
  867. ubh_mark_buffer_dirty(ubh);
  868. free_full_branch(inode, block, depth);
  869. }
  870. }
  871. } else {
  872. struct to_free ctx = {.inode = inode};
  873. for (i = from; i < uspi->s_apb; i++) {
  874. void *p = ubh_get_data_ptr(uspi, ubh, i);
  875. u64 block = ufs_data_ptr_to_cpu(sb, p);
  876. if (block) {
  877. write_seqlock(&UFS_I(inode)->meta_lock);
  878. ufs_data_ptr_clear(uspi, p);
  879. write_sequnlock(&UFS_I(inode)->meta_lock);
  880. ubh_mark_buffer_dirty(ubh);
  881. free_data(&ctx, block, uspi->s_fpb);
  882. }
  883. }
  884. free_data(&ctx, 0, 0);
  885. }
  886. if (IS_SYNC(inode) && ubh_buffer_dirty(ubh))
  887. ubh_sync_block(ubh);
  888. ubh_brelse(ubh);
  889. }
  890. static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
  891. {
  892. int err = 0;
  893. struct super_block *sb = inode->i_sb;
  894. struct address_space *mapping = inode->i_mapping;
  895. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  896. unsigned i, end;
  897. sector_t lastfrag;
  898. struct page *lastpage;
  899. struct buffer_head *bh;
  900. u64 phys64;
  901. lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
  902. if (!lastfrag)
  903. goto out;
  904. lastfrag--;
  905. lastpage = ufs_get_locked_page(mapping, lastfrag >>
  906. (PAGE_CACHE_SHIFT - inode->i_blkbits));
  907. if (IS_ERR(lastpage)) {
  908. err = -EIO;
  909. goto out;
  910. }
  911. end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
  912. bh = page_buffers(lastpage);
  913. for (i = 0; i < end; ++i)
  914. bh = bh->b_this_page;
  915. err = ufs_getfrag_block(inode, lastfrag, bh, 1);
  916. if (unlikely(err))
  917. goto out_unlock;
  918. if (buffer_new(bh)) {
  919. clear_buffer_new(bh);
  920. unmap_underlying_metadata(bh->b_bdev,
  921. bh->b_blocknr);
  922. /*
  923. * we do not zeroize fragment, because of
  924. * if it maped to hole, it already contains zeroes
  925. */
  926. set_buffer_uptodate(bh);
  927. mark_buffer_dirty(bh);
  928. set_page_dirty(lastpage);
  929. }
  930. if (lastfrag >= UFS_IND_FRAGMENT) {
  931. end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
  932. phys64 = bh->b_blocknr + 1;
  933. for (i = 0; i < end; ++i) {
  934. bh = sb_getblk(sb, i + phys64);
  935. lock_buffer(bh);
  936. memset(bh->b_data, 0, sb->s_blocksize);
  937. set_buffer_uptodate(bh);
  938. mark_buffer_dirty(bh);
  939. unlock_buffer(bh);
  940. sync_dirty_buffer(bh);
  941. brelse(bh);
  942. }
  943. }
  944. out_unlock:
  945. ufs_put_locked_page(lastpage);
  946. out:
  947. return err;
  948. }
  949. static void __ufs_truncate_blocks(struct inode *inode)
  950. {
  951. struct ufs_inode_info *ufsi = UFS_I(inode);
  952. struct super_block *sb = inode->i_sb;
  953. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  954. unsigned offsets[4];
  955. int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets);
  956. int depth2;
  957. unsigned i;
  958. struct ufs_buffer_head *ubh[3];
  959. void *p;
  960. u64 block;
  961. if (!depth)
  962. return;
  963. /* find the last non-zero in offsets[] */
  964. for (depth2 = depth - 1; depth2; depth2--)
  965. if (offsets[depth2])
  966. break;
  967. mutex_lock(&ufsi->truncate_mutex);
  968. if (depth == 1) {
  969. ufs_trunc_direct(inode);
  970. offsets[0] = UFS_IND_BLOCK;
  971. } else {
  972. /* get the blocks that should be partially emptied */
  973. p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]);
  974. for (i = 0; i < depth2; i++) {
  975. offsets[i]++; /* next branch is fully freed */
  976. block = ufs_data_ptr_to_cpu(sb, p);
  977. if (!block)
  978. break;
  979. ubh[i] = ubh_bread(sb, block, uspi->s_bsize);
  980. if (!ubh[i]) {
  981. write_seqlock(&ufsi->meta_lock);
  982. ufs_data_ptr_clear(uspi, p);
  983. write_sequnlock(&ufsi->meta_lock);
  984. break;
  985. }
  986. p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]);
  987. }
  988. while (i--)
  989. free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1);
  990. }
  991. for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) {
  992. p = ufs_get_direct_data_ptr(uspi, ufsi, i);
  993. block = ufs_data_ptr_to_cpu(sb, p);
  994. if (block) {
  995. write_seqlock(&ufsi->meta_lock);
  996. ufs_data_ptr_clear(uspi, p);
  997. write_sequnlock(&ufsi->meta_lock);
  998. free_full_branch(inode, block, i - UFS_IND_BLOCK + 1);
  999. }
  1000. }
  1001. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  1002. mark_inode_dirty(inode);
  1003. mutex_unlock(&ufsi->truncate_mutex);
  1004. }
  1005. static int ufs_truncate(struct inode *inode, loff_t size)
  1006. {
  1007. int err = 0;
  1008. UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
  1009. inode->i_ino, (unsigned long long)size,
  1010. (unsigned long long)i_size_read(inode));
  1011. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1012. S_ISLNK(inode->i_mode)))
  1013. return -EINVAL;
  1014. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1015. return -EPERM;
  1016. err = ufs_alloc_lastblock(inode, size);
  1017. if (err)
  1018. goto out;
  1019. block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
  1020. truncate_setsize(inode, size);
  1021. __ufs_truncate_blocks(inode);
  1022. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1023. mark_inode_dirty(inode);
  1024. out:
  1025. UFSD("EXIT: err %d\n", err);
  1026. return err;
  1027. }
  1028. void ufs_truncate_blocks(struct inode *inode)
  1029. {
  1030. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1031. S_ISLNK(inode->i_mode)))
  1032. return;
  1033. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1034. return;
  1035. __ufs_truncate_blocks(inode);
  1036. }
  1037. int ufs_setattr(struct dentry *dentry, struct iattr *attr)
  1038. {
  1039. struct inode *inode = d_inode(dentry);
  1040. unsigned int ia_valid = attr->ia_valid;
  1041. int error;
  1042. error = inode_change_ok(inode, attr);
  1043. if (error)
  1044. return error;
  1045. if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
  1046. error = ufs_truncate(inode, attr->ia_size);
  1047. if (error)
  1048. return error;
  1049. }
  1050. setattr_copy(inode, attr);
  1051. mark_inode_dirty(inode);
  1052. return 0;
  1053. }
  1054. const struct inode_operations ufs_file_inode_operations = {
  1055. .setattr = ufs_setattr,
  1056. };