inode.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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(inode);
  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(inode);
  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_link = (char *)UFS_I(inode)->i_u1.i_symlink;
  469. inode->i_op = &simple_symlink_inode_operations;
  470. } else {
  471. inode->i_mapping->a_ops = &ufs_aops;
  472. inode->i_op = &page_symlink_inode_operations;
  473. inode_nohighmem(inode);
  474. }
  475. } else
  476. init_special_inode(inode, inode->i_mode,
  477. ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
  478. }
  479. static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  480. {
  481. struct ufs_inode_info *ufsi = UFS_I(inode);
  482. struct super_block *sb = inode->i_sb;
  483. umode_t mode;
  484. /*
  485. * Copy data to the in-core inode.
  486. */
  487. inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
  488. set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
  489. if (inode->i_nlink == 0) {
  490. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  491. return -1;
  492. }
  493. /*
  494. * Linux now has 32-bit uid and gid, so we can support EFT.
  495. */
  496. i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
  497. i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
  498. inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
  499. inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
  500. inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
  501. inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
  502. inode->i_mtime.tv_nsec = 0;
  503. inode->i_atime.tv_nsec = 0;
  504. inode->i_ctime.tv_nsec = 0;
  505. inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
  506. inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
  507. ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
  508. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  509. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  510. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  511. memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
  512. sizeof(ufs_inode->ui_u2.ui_addr));
  513. } else {
  514. memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
  515. sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
  516. ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
  517. }
  518. return 0;
  519. }
  520. static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
  521. {
  522. struct ufs_inode_info *ufsi = UFS_I(inode);
  523. struct super_block *sb = inode->i_sb;
  524. umode_t mode;
  525. UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
  526. /*
  527. * Copy data to the in-core inode.
  528. */
  529. inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
  530. set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
  531. if (inode->i_nlink == 0) {
  532. ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
  533. return -1;
  534. }
  535. /*
  536. * Linux now has 32-bit uid and gid, so we can support EFT.
  537. */
  538. i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
  539. i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
  540. inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
  541. inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
  542. inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
  543. inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
  544. inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
  545. inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
  546. inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
  547. inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
  548. inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
  549. ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
  550. /*
  551. ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
  552. ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
  553. */
  554. if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
  555. memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
  556. sizeof(ufs2_inode->ui_u2.ui_addr));
  557. } else {
  558. memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
  559. sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
  560. ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
  561. }
  562. return 0;
  563. }
  564. struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
  565. {
  566. struct ufs_inode_info *ufsi;
  567. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  568. struct buffer_head * bh;
  569. struct inode *inode;
  570. int err;
  571. UFSD("ENTER, ino %lu\n", ino);
  572. if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
  573. ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
  574. ino);
  575. return ERR_PTR(-EIO);
  576. }
  577. inode = iget_locked(sb, ino);
  578. if (!inode)
  579. return ERR_PTR(-ENOMEM);
  580. if (!(inode->i_state & I_NEW))
  581. return inode;
  582. ufsi = UFS_I(inode);
  583. bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
  584. if (!bh) {
  585. ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
  586. inode->i_ino);
  587. goto bad_inode;
  588. }
  589. if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
  590. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  591. err = ufs2_read_inode(inode,
  592. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  593. } else {
  594. struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
  595. err = ufs1_read_inode(inode,
  596. ufs_inode + ufs_inotofsbo(inode->i_ino));
  597. }
  598. if (err)
  599. goto bad_inode;
  600. inode->i_version++;
  601. ufsi->i_lastfrag =
  602. (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
  603. ufsi->i_dir_start_lookup = 0;
  604. ufsi->i_osync = 0;
  605. ufs_set_inode_ops(inode);
  606. brelse(bh);
  607. UFSD("EXIT\n");
  608. unlock_new_inode(inode);
  609. return inode;
  610. bad_inode:
  611. iget_failed(inode);
  612. return ERR_PTR(-EIO);
  613. }
  614. static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
  615. {
  616. struct super_block *sb = inode->i_sb;
  617. struct ufs_inode_info *ufsi = UFS_I(inode);
  618. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  619. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  620. ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
  621. ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
  622. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  623. ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
  624. ufs_inode->ui_atime.tv_usec = 0;
  625. ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
  626. ufs_inode->ui_ctime.tv_usec = 0;
  627. ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
  628. ufs_inode->ui_mtime.tv_usec = 0;
  629. ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
  630. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  631. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  632. if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
  633. ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
  634. ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
  635. }
  636. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  637. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  638. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
  639. } else if (inode->i_blocks) {
  640. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
  641. sizeof(ufs_inode->ui_u2.ui_addr));
  642. }
  643. else {
  644. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  645. sizeof(ufs_inode->ui_u2.ui_symlink));
  646. }
  647. if (!inode->i_nlink)
  648. memset (ufs_inode, 0, sizeof(struct ufs_inode));
  649. }
  650. static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
  651. {
  652. struct super_block *sb = inode->i_sb;
  653. struct ufs_inode_info *ufsi = UFS_I(inode);
  654. UFSD("ENTER\n");
  655. ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
  656. ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
  657. ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
  658. ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
  659. ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
  660. ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
  661. ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
  662. ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
  663. ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
  664. ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
  665. ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
  666. ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
  667. ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
  668. ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
  669. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  670. /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
  671. ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
  672. } else if (inode->i_blocks) {
  673. memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
  674. sizeof(ufs_inode->ui_u2.ui_addr));
  675. } else {
  676. memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
  677. sizeof(ufs_inode->ui_u2.ui_symlink));
  678. }
  679. if (!inode->i_nlink)
  680. memset (ufs_inode, 0, sizeof(struct ufs2_inode));
  681. UFSD("EXIT\n");
  682. }
  683. static int ufs_update_inode(struct inode * inode, int do_sync)
  684. {
  685. struct super_block *sb = inode->i_sb;
  686. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  687. struct buffer_head * bh;
  688. UFSD("ENTER, ino %lu\n", inode->i_ino);
  689. if (inode->i_ino < UFS_ROOTINO ||
  690. inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
  691. ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
  692. return -1;
  693. }
  694. bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
  695. if (!bh) {
  696. ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
  697. return -1;
  698. }
  699. if (uspi->fs_magic == UFS2_MAGIC) {
  700. struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
  701. ufs2_update_inode(inode,
  702. ufs2_inode + ufs_inotofsbo(inode->i_ino));
  703. } else {
  704. struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
  705. ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
  706. }
  707. mark_buffer_dirty(bh);
  708. if (do_sync)
  709. sync_dirty_buffer(bh);
  710. brelse (bh);
  711. UFSD("EXIT\n");
  712. return 0;
  713. }
  714. int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
  715. {
  716. return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  717. }
  718. int ufs_sync_inode (struct inode *inode)
  719. {
  720. return ufs_update_inode (inode, 1);
  721. }
  722. void ufs_evict_inode(struct inode * inode)
  723. {
  724. int want_delete = 0;
  725. if (!inode->i_nlink && !is_bad_inode(inode))
  726. want_delete = 1;
  727. truncate_inode_pages_final(&inode->i_data);
  728. if (want_delete) {
  729. inode->i_size = 0;
  730. if (inode->i_blocks)
  731. ufs_truncate_blocks(inode);
  732. }
  733. invalidate_inode_buffers(inode);
  734. clear_inode(inode);
  735. if (want_delete)
  736. ufs_free_inode(inode);
  737. }
  738. struct to_free {
  739. struct inode *inode;
  740. u64 to;
  741. unsigned count;
  742. };
  743. static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
  744. {
  745. if (ctx->count && ctx->to != from) {
  746. ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
  747. ctx->count = 0;
  748. }
  749. ctx->count += count;
  750. ctx->to = from + count;
  751. }
  752. #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
  753. #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
  754. static void ufs_trunc_direct(struct inode *inode)
  755. {
  756. struct ufs_inode_info *ufsi = UFS_I(inode);
  757. struct super_block * sb;
  758. struct ufs_sb_private_info * uspi;
  759. void *p;
  760. u64 frag1, frag2, frag3, frag4, block1, block2;
  761. struct to_free ctx = {.inode = inode};
  762. unsigned i, tmp;
  763. UFSD("ENTER: ino %lu\n", inode->i_ino);
  764. sb = inode->i_sb;
  765. uspi = UFS_SB(sb)->s_uspi;
  766. frag1 = DIRECT_FRAGMENT;
  767. frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
  768. frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
  769. frag3 = frag4 & ~uspi->s_fpbmask;
  770. block1 = block2 = 0;
  771. if (frag2 > frag3) {
  772. frag2 = frag4;
  773. frag3 = frag4 = 0;
  774. } else if (frag2 < frag3) {
  775. block1 = ufs_fragstoblks (frag2);
  776. block2 = ufs_fragstoblks (frag3);
  777. }
  778. UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
  779. " frag3 %llu, frag4 %llu\n", inode->i_ino,
  780. (unsigned long long)frag1, (unsigned long long)frag2,
  781. (unsigned long long)block1, (unsigned long long)block2,
  782. (unsigned long long)frag3, (unsigned long long)frag4);
  783. if (frag1 >= frag2)
  784. goto next1;
  785. /*
  786. * Free first free fragments
  787. */
  788. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
  789. tmp = ufs_data_ptr_to_cpu(sb, p);
  790. if (!tmp )
  791. ufs_panic (sb, "ufs_trunc_direct", "internal error");
  792. frag2 -= frag1;
  793. frag1 = ufs_fragnum (frag1);
  794. ufs_free_fragments(inode, tmp + frag1, frag2);
  795. next1:
  796. /*
  797. * Free whole blocks
  798. */
  799. for (i = block1 ; i < block2; i++) {
  800. p = ufs_get_direct_data_ptr(uspi, ufsi, i);
  801. tmp = ufs_data_ptr_to_cpu(sb, p);
  802. if (!tmp)
  803. continue;
  804. write_seqlock(&ufsi->meta_lock);
  805. ufs_data_ptr_clear(uspi, p);
  806. write_sequnlock(&ufsi->meta_lock);
  807. free_data(&ctx, tmp, uspi->s_fpb);
  808. }
  809. free_data(&ctx, 0, 0);
  810. if (frag3 >= frag4)
  811. goto next3;
  812. /*
  813. * Free last free fragments
  814. */
  815. p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
  816. tmp = ufs_data_ptr_to_cpu(sb, p);
  817. if (!tmp )
  818. ufs_panic(sb, "ufs_truncate_direct", "internal error");
  819. frag4 = ufs_fragnum (frag4);
  820. write_seqlock(&ufsi->meta_lock);
  821. ufs_data_ptr_clear(uspi, p);
  822. write_sequnlock(&ufsi->meta_lock);
  823. ufs_free_fragments (inode, tmp, frag4);
  824. next3:
  825. UFSD("EXIT: ino %lu\n", inode->i_ino);
  826. }
  827. static void free_full_branch(struct inode *inode, u64 ind_block, int depth)
  828. {
  829. struct super_block *sb = inode->i_sb;
  830. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  831. struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize);
  832. unsigned i;
  833. if (!ubh)
  834. return;
  835. if (--depth) {
  836. for (i = 0; i < uspi->s_apb; i++) {
  837. void *p = ubh_get_data_ptr(uspi, ubh, i);
  838. u64 block = ufs_data_ptr_to_cpu(sb, p);
  839. if (block)
  840. free_full_branch(inode, block, depth);
  841. }
  842. } else {
  843. struct to_free ctx = {.inode = inode};
  844. for (i = 0; i < uspi->s_apb; i++) {
  845. void *p = ubh_get_data_ptr(uspi, ubh, i);
  846. u64 block = ufs_data_ptr_to_cpu(sb, p);
  847. if (block)
  848. free_data(&ctx, block, uspi->s_fpb);
  849. }
  850. free_data(&ctx, 0, 0);
  851. }
  852. ubh_bforget(ubh);
  853. ufs_free_blocks(inode, ind_block, uspi->s_fpb);
  854. }
  855. static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth)
  856. {
  857. struct super_block *sb = inode->i_sb;
  858. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  859. unsigned i;
  860. if (--depth) {
  861. for (i = from; i < uspi->s_apb ; i++) {
  862. void *p = ubh_get_data_ptr(uspi, ubh, i);
  863. u64 block = ufs_data_ptr_to_cpu(sb, p);
  864. if (block) {
  865. write_seqlock(&UFS_I(inode)->meta_lock);
  866. ufs_data_ptr_clear(uspi, p);
  867. write_sequnlock(&UFS_I(inode)->meta_lock);
  868. ubh_mark_buffer_dirty(ubh);
  869. free_full_branch(inode, block, depth);
  870. }
  871. }
  872. } else {
  873. struct to_free ctx = {.inode = inode};
  874. for (i = from; i < uspi->s_apb; i++) {
  875. void *p = ubh_get_data_ptr(uspi, ubh, i);
  876. u64 block = ufs_data_ptr_to_cpu(sb, p);
  877. if (block) {
  878. write_seqlock(&UFS_I(inode)->meta_lock);
  879. ufs_data_ptr_clear(uspi, p);
  880. write_sequnlock(&UFS_I(inode)->meta_lock);
  881. ubh_mark_buffer_dirty(ubh);
  882. free_data(&ctx, block, uspi->s_fpb);
  883. }
  884. }
  885. free_data(&ctx, 0, 0);
  886. }
  887. if (IS_SYNC(inode) && ubh_buffer_dirty(ubh))
  888. ubh_sync_block(ubh);
  889. ubh_brelse(ubh);
  890. }
  891. static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
  892. {
  893. int err = 0;
  894. struct super_block *sb = inode->i_sb;
  895. struct address_space *mapping = inode->i_mapping;
  896. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  897. unsigned i, end;
  898. sector_t lastfrag;
  899. struct page *lastpage;
  900. struct buffer_head *bh;
  901. u64 phys64;
  902. lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
  903. if (!lastfrag)
  904. goto out;
  905. lastfrag--;
  906. lastpage = ufs_get_locked_page(mapping, lastfrag >>
  907. (PAGE_SHIFT - inode->i_blkbits));
  908. if (IS_ERR(lastpage)) {
  909. err = -EIO;
  910. goto out;
  911. }
  912. end = lastfrag & ((1 << (PAGE_SHIFT - inode->i_blkbits)) - 1);
  913. bh = page_buffers(lastpage);
  914. for (i = 0; i < end; ++i)
  915. bh = bh->b_this_page;
  916. err = ufs_getfrag_block(inode, lastfrag, bh, 1);
  917. if (unlikely(err))
  918. goto out_unlock;
  919. if (buffer_new(bh)) {
  920. clear_buffer_new(bh);
  921. unmap_underlying_metadata(bh->b_bdev,
  922. bh->b_blocknr);
  923. /*
  924. * we do not zeroize fragment, because of
  925. * if it maped to hole, it already contains zeroes
  926. */
  927. set_buffer_uptodate(bh);
  928. mark_buffer_dirty(bh);
  929. set_page_dirty(lastpage);
  930. }
  931. if (lastfrag >= UFS_IND_FRAGMENT) {
  932. end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
  933. phys64 = bh->b_blocknr + 1;
  934. for (i = 0; i < end; ++i) {
  935. bh = sb_getblk(sb, i + phys64);
  936. lock_buffer(bh);
  937. memset(bh->b_data, 0, sb->s_blocksize);
  938. set_buffer_uptodate(bh);
  939. mark_buffer_dirty(bh);
  940. unlock_buffer(bh);
  941. sync_dirty_buffer(bh);
  942. brelse(bh);
  943. }
  944. }
  945. out_unlock:
  946. ufs_put_locked_page(lastpage);
  947. out:
  948. return err;
  949. }
  950. static void __ufs_truncate_blocks(struct inode *inode)
  951. {
  952. struct ufs_inode_info *ufsi = UFS_I(inode);
  953. struct super_block *sb = inode->i_sb;
  954. struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
  955. unsigned offsets[4];
  956. int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets);
  957. int depth2;
  958. unsigned i;
  959. struct ufs_buffer_head *ubh[3];
  960. void *p;
  961. u64 block;
  962. if (!depth)
  963. return;
  964. /* find the last non-zero in offsets[] */
  965. for (depth2 = depth - 1; depth2; depth2--)
  966. if (offsets[depth2])
  967. break;
  968. mutex_lock(&ufsi->truncate_mutex);
  969. if (depth == 1) {
  970. ufs_trunc_direct(inode);
  971. offsets[0] = UFS_IND_BLOCK;
  972. } else {
  973. /* get the blocks that should be partially emptied */
  974. p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]);
  975. for (i = 0; i < depth2; i++) {
  976. offsets[i]++; /* next branch is fully freed */
  977. block = ufs_data_ptr_to_cpu(sb, p);
  978. if (!block)
  979. break;
  980. ubh[i] = ubh_bread(sb, block, uspi->s_bsize);
  981. if (!ubh[i]) {
  982. write_seqlock(&ufsi->meta_lock);
  983. ufs_data_ptr_clear(uspi, p);
  984. write_sequnlock(&ufsi->meta_lock);
  985. break;
  986. }
  987. p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]);
  988. }
  989. while (i--)
  990. free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1);
  991. }
  992. for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) {
  993. p = ufs_get_direct_data_ptr(uspi, ufsi, i);
  994. block = ufs_data_ptr_to_cpu(sb, p);
  995. if (block) {
  996. write_seqlock(&ufsi->meta_lock);
  997. ufs_data_ptr_clear(uspi, p);
  998. write_sequnlock(&ufsi->meta_lock);
  999. free_full_branch(inode, block, i - UFS_IND_BLOCK + 1);
  1000. }
  1001. }
  1002. ufsi->i_lastfrag = DIRECT_FRAGMENT;
  1003. mark_inode_dirty(inode);
  1004. mutex_unlock(&ufsi->truncate_mutex);
  1005. }
  1006. static int ufs_truncate(struct inode *inode, loff_t size)
  1007. {
  1008. int err = 0;
  1009. UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
  1010. inode->i_ino, (unsigned long long)size,
  1011. (unsigned long long)i_size_read(inode));
  1012. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1013. S_ISLNK(inode->i_mode)))
  1014. return -EINVAL;
  1015. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1016. return -EPERM;
  1017. err = ufs_alloc_lastblock(inode, size);
  1018. if (err)
  1019. goto out;
  1020. block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
  1021. truncate_setsize(inode, size);
  1022. __ufs_truncate_blocks(inode);
  1023. inode->i_mtime = inode->i_ctime = current_time(inode);
  1024. mark_inode_dirty(inode);
  1025. out:
  1026. UFSD("EXIT: err %d\n", err);
  1027. return err;
  1028. }
  1029. void ufs_truncate_blocks(struct inode *inode)
  1030. {
  1031. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1032. S_ISLNK(inode->i_mode)))
  1033. return;
  1034. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1035. return;
  1036. __ufs_truncate_blocks(inode);
  1037. }
  1038. int ufs_setattr(struct dentry *dentry, struct iattr *attr)
  1039. {
  1040. struct inode *inode = d_inode(dentry);
  1041. unsigned int ia_valid = attr->ia_valid;
  1042. int error;
  1043. error = setattr_prepare(dentry, attr);
  1044. if (error)
  1045. return error;
  1046. if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
  1047. error = ufs_truncate(inode, attr->ia_size);
  1048. if (error)
  1049. return error;
  1050. }
  1051. setattr_copy(inode, attr);
  1052. mark_inode_dirty(inode);
  1053. return 0;
  1054. }
  1055. const struct inode_operations ufs_file_inode_operations = {
  1056. .setattr = ufs_setattr,
  1057. };