inode.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * inode.c - NILFS inode operations.
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. *
  22. */
  23. #include <linux/buffer_head.h>
  24. #include <linux/gfp.h>
  25. #include <linux/mpage.h>
  26. #include <linux/writeback.h>
  27. #include <linux/uio.h>
  28. #include "nilfs.h"
  29. #include "btnode.h"
  30. #include "segment.h"
  31. #include "page.h"
  32. #include "mdt.h"
  33. #include "cpfile.h"
  34. #include "ifile.h"
  35. struct nilfs_iget_args {
  36. u64 ino;
  37. __u64 cno;
  38. struct nilfs_root *root;
  39. int for_gc;
  40. };
  41. /**
  42. * nilfs_get_block() - get a file block on the filesystem (callback function)
  43. * @inode - inode struct of the target file
  44. * @blkoff - file block number
  45. * @bh_result - buffer head to be mapped on
  46. * @create - indicate whether allocating the block or not when it has not
  47. * been allocated yet.
  48. *
  49. * This function does not issue actual read request of the specified data
  50. * block. It is done by VFS.
  51. */
  52. int nilfs_get_block(struct inode *inode, sector_t blkoff,
  53. struct buffer_head *bh_result, int create)
  54. {
  55. struct nilfs_inode_info *ii = NILFS_I(inode);
  56. __u64 blknum = 0;
  57. int err = 0, ret;
  58. struct inode *dat = NILFS_I_NILFS(inode)->ns_dat;
  59. unsigned maxblocks = bh_result->b_size >> inode->i_blkbits;
  60. down_read(&NILFS_MDT(dat)->mi_sem);
  61. ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks);
  62. up_read(&NILFS_MDT(dat)->mi_sem);
  63. if (ret >= 0) { /* found */
  64. map_bh(bh_result, inode->i_sb, blknum);
  65. if (ret > 0)
  66. bh_result->b_size = (ret << inode->i_blkbits);
  67. goto out;
  68. }
  69. /* data block was not found */
  70. if (ret == -ENOENT && create) {
  71. struct nilfs_transaction_info ti;
  72. bh_result->b_blocknr = 0;
  73. err = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  74. if (unlikely(err))
  75. goto out;
  76. err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff,
  77. (unsigned long)bh_result);
  78. if (unlikely(err != 0)) {
  79. if (err == -EEXIST) {
  80. /*
  81. * The get_block() function could be called
  82. * from multiple callers for an inode.
  83. * However, the page having this block must
  84. * be locked in this case.
  85. */
  86. printk(KERN_WARNING
  87. "nilfs_get_block: a race condition "
  88. "while inserting a data block. "
  89. "(inode number=%lu, file block "
  90. "offset=%llu)\n",
  91. inode->i_ino,
  92. (unsigned long long)blkoff);
  93. err = 0;
  94. }
  95. nilfs_transaction_abort(inode->i_sb);
  96. goto out;
  97. }
  98. nilfs_mark_inode_dirty(inode);
  99. nilfs_transaction_commit(inode->i_sb); /* never fails */
  100. /* Error handling should be detailed */
  101. set_buffer_new(bh_result);
  102. set_buffer_delay(bh_result);
  103. map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed
  104. to proper value */
  105. } else if (ret == -ENOENT) {
  106. /* not found is not error (e.g. hole); must return without
  107. the mapped state flag. */
  108. ;
  109. } else {
  110. err = ret;
  111. }
  112. out:
  113. return err;
  114. }
  115. /**
  116. * nilfs_readpage() - implement readpage() method of nilfs_aops {}
  117. * address_space_operations.
  118. * @file - file struct of the file to be read
  119. * @page - the page to be read
  120. */
  121. static int nilfs_readpage(struct file *file, struct page *page)
  122. {
  123. return mpage_readpage(page, nilfs_get_block);
  124. }
  125. /**
  126. * nilfs_readpages() - implement readpages() method of nilfs_aops {}
  127. * address_space_operations.
  128. * @file - file struct of the file to be read
  129. * @mapping - address_space struct used for reading multiple pages
  130. * @pages - the pages to be read
  131. * @nr_pages - number of pages to be read
  132. */
  133. static int nilfs_readpages(struct file *file, struct address_space *mapping,
  134. struct list_head *pages, unsigned nr_pages)
  135. {
  136. return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block);
  137. }
  138. static int nilfs_writepages(struct address_space *mapping,
  139. struct writeback_control *wbc)
  140. {
  141. struct inode *inode = mapping->host;
  142. int err = 0;
  143. if (wbc->sync_mode == WB_SYNC_ALL)
  144. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  145. wbc->range_start,
  146. wbc->range_end);
  147. return err;
  148. }
  149. static int nilfs_writepage(struct page *page, struct writeback_control *wbc)
  150. {
  151. struct inode *inode = page->mapping->host;
  152. int err;
  153. redirty_page_for_writepage(wbc, page);
  154. unlock_page(page);
  155. if (wbc->sync_mode == WB_SYNC_ALL) {
  156. err = nilfs_construct_segment(inode->i_sb);
  157. if (unlikely(err))
  158. return err;
  159. } else if (wbc->for_reclaim)
  160. nilfs_flush_segment(inode->i_sb, inode->i_ino);
  161. return 0;
  162. }
  163. static int nilfs_set_page_dirty(struct page *page)
  164. {
  165. int ret = __set_page_dirty_buffers(page);
  166. if (ret) {
  167. struct inode *inode = page->mapping->host;
  168. unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits);
  169. nilfs_set_file_dirty(inode, nr_dirty);
  170. }
  171. return ret;
  172. }
  173. static int nilfs_write_begin(struct file *file, struct address_space *mapping,
  174. loff_t pos, unsigned len, unsigned flags,
  175. struct page **pagep, void **fsdata)
  176. {
  177. struct inode *inode = mapping->host;
  178. int err = nilfs_transaction_begin(inode->i_sb, NULL, 1);
  179. if (unlikely(err))
  180. return err;
  181. err = block_write_begin(mapping, pos, len, flags, pagep,
  182. nilfs_get_block);
  183. if (unlikely(err)) {
  184. loff_t isize = mapping->host->i_size;
  185. if (pos + len > isize)
  186. vmtruncate(mapping->host, isize);
  187. nilfs_transaction_abort(inode->i_sb);
  188. }
  189. return err;
  190. }
  191. static int nilfs_write_end(struct file *file, struct address_space *mapping,
  192. loff_t pos, unsigned len, unsigned copied,
  193. struct page *page, void *fsdata)
  194. {
  195. struct inode *inode = mapping->host;
  196. unsigned start = pos & (PAGE_CACHE_SIZE - 1);
  197. unsigned nr_dirty;
  198. int err;
  199. nr_dirty = nilfs_page_count_clean_buffers(page, start,
  200. start + copied);
  201. copied = generic_write_end(file, mapping, pos, len, copied, page,
  202. fsdata);
  203. nilfs_set_file_dirty(inode, nr_dirty);
  204. err = nilfs_transaction_commit(inode->i_sb);
  205. return err ? : copied;
  206. }
  207. static ssize_t
  208. nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  209. loff_t offset, unsigned long nr_segs)
  210. {
  211. struct file *file = iocb->ki_filp;
  212. struct inode *inode = file->f_mapping->host;
  213. ssize_t size;
  214. if (rw == WRITE)
  215. return 0;
  216. /* Needs synchronization with the cleaner */
  217. size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  218. offset, nr_segs, nilfs_get_block, NULL);
  219. /*
  220. * In case of error extending write may have instantiated a few
  221. * blocks outside i_size. Trim these off again.
  222. */
  223. if (unlikely((rw & WRITE) && size < 0)) {
  224. loff_t isize = i_size_read(inode);
  225. loff_t end = offset + iov_length(iov, nr_segs);
  226. if (end > isize)
  227. vmtruncate(inode, isize);
  228. }
  229. return size;
  230. }
  231. const struct address_space_operations nilfs_aops = {
  232. .writepage = nilfs_writepage,
  233. .readpage = nilfs_readpage,
  234. .sync_page = block_sync_page,
  235. .writepages = nilfs_writepages,
  236. .set_page_dirty = nilfs_set_page_dirty,
  237. .readpages = nilfs_readpages,
  238. .write_begin = nilfs_write_begin,
  239. .write_end = nilfs_write_end,
  240. /* .releasepage = nilfs_releasepage, */
  241. .invalidatepage = block_invalidatepage,
  242. .direct_IO = nilfs_direct_IO,
  243. .is_partially_uptodate = block_is_partially_uptodate,
  244. };
  245. struct inode *nilfs_new_inode(struct inode *dir, int mode)
  246. {
  247. struct super_block *sb = dir->i_sb;
  248. struct nilfs_sb_info *sbi = NILFS_SB(sb);
  249. struct inode *inode;
  250. struct nilfs_inode_info *ii;
  251. struct nilfs_root *root;
  252. int err = -ENOMEM;
  253. ino_t ino;
  254. inode = new_inode(sb);
  255. if (unlikely(!inode))
  256. goto failed;
  257. mapping_set_gfp_mask(inode->i_mapping,
  258. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  259. root = NILFS_I(dir)->i_root;
  260. ii = NILFS_I(inode);
  261. ii->i_state = 1 << NILFS_I_NEW;
  262. ii->i_root = root;
  263. err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh);
  264. if (unlikely(err))
  265. goto failed_ifile_create_inode;
  266. /* reference count of i_bh inherits from nilfs_mdt_read_block() */
  267. atomic_inc(&root->inodes_count);
  268. inode_init_owner(inode, dir, mode);
  269. inode->i_ino = ino;
  270. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  271. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
  272. err = nilfs_bmap_read(ii->i_bmap, NULL);
  273. if (err < 0)
  274. goto failed_bmap;
  275. set_bit(NILFS_I_BMAP, &ii->i_state);
  276. /* No lock is needed; iget() ensures it. */
  277. }
  278. ii->i_flags = NILFS_I(dir)->i_flags;
  279. if (S_ISLNK(mode))
  280. ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL);
  281. if (!S_ISDIR(mode))
  282. ii->i_flags &= ~NILFS_DIRSYNC_FL;
  283. /* ii->i_file_acl = 0; */
  284. /* ii->i_dir_acl = 0; */
  285. ii->i_dir_start_lookup = 0;
  286. nilfs_set_inode_flags(inode);
  287. spin_lock(&sbi->s_next_gen_lock);
  288. inode->i_generation = sbi->s_next_generation++;
  289. spin_unlock(&sbi->s_next_gen_lock);
  290. insert_inode_hash(inode);
  291. err = nilfs_init_acl(inode, dir);
  292. if (unlikely(err))
  293. goto failed_acl; /* never occur. When supporting
  294. nilfs_init_acl(), proper cancellation of
  295. above jobs should be considered */
  296. return inode;
  297. failed_acl:
  298. failed_bmap:
  299. inode->i_nlink = 0;
  300. iput(inode); /* raw_inode will be deleted through
  301. generic_delete_inode() */
  302. goto failed;
  303. failed_ifile_create_inode:
  304. make_bad_inode(inode);
  305. iput(inode); /* if i_nlink == 1, generic_forget_inode() will be
  306. called */
  307. failed:
  308. return ERR_PTR(err);
  309. }
  310. void nilfs_set_inode_flags(struct inode *inode)
  311. {
  312. unsigned int flags = NILFS_I(inode)->i_flags;
  313. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  314. S_DIRSYNC);
  315. if (flags & NILFS_SYNC_FL)
  316. inode->i_flags |= S_SYNC;
  317. if (flags & NILFS_APPEND_FL)
  318. inode->i_flags |= S_APPEND;
  319. if (flags & NILFS_IMMUTABLE_FL)
  320. inode->i_flags |= S_IMMUTABLE;
  321. #ifndef NILFS_ATIME_DISABLE
  322. if (flags & NILFS_NOATIME_FL)
  323. #endif
  324. inode->i_flags |= S_NOATIME;
  325. if (flags & NILFS_DIRSYNC_FL)
  326. inode->i_flags |= S_DIRSYNC;
  327. mapping_set_gfp_mask(inode->i_mapping,
  328. mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS);
  329. }
  330. int nilfs_read_inode_common(struct inode *inode,
  331. struct nilfs_inode *raw_inode)
  332. {
  333. struct nilfs_inode_info *ii = NILFS_I(inode);
  334. int err;
  335. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  336. inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid);
  337. inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid);
  338. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  339. inode->i_size = le64_to_cpu(raw_inode->i_size);
  340. inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  341. inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime);
  342. inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime);
  343. inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  344. inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
  345. inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
  346. if (inode->i_nlink == 0 && inode->i_mode == 0)
  347. return -EINVAL; /* this inode is deleted */
  348. inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
  349. ii->i_flags = le32_to_cpu(raw_inode->i_flags);
  350. #if 0
  351. ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  352. ii->i_dir_acl = S_ISREG(inode->i_mode) ?
  353. 0 : le32_to_cpu(raw_inode->i_dir_acl);
  354. #endif
  355. ii->i_dir_start_lookup = 0;
  356. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  357. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  358. S_ISLNK(inode->i_mode)) {
  359. err = nilfs_bmap_read(ii->i_bmap, raw_inode);
  360. if (err < 0)
  361. return err;
  362. set_bit(NILFS_I_BMAP, &ii->i_state);
  363. /* No lock is needed; iget() ensures it. */
  364. }
  365. return 0;
  366. }
  367. static int __nilfs_read_inode(struct super_block *sb,
  368. struct nilfs_root *root, unsigned long ino,
  369. struct inode *inode)
  370. {
  371. struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
  372. struct buffer_head *bh;
  373. struct nilfs_inode *raw_inode;
  374. int err;
  375. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  376. err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh);
  377. if (unlikely(err))
  378. goto bad_inode;
  379. raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh);
  380. err = nilfs_read_inode_common(inode, raw_inode);
  381. if (err)
  382. goto failed_unmap;
  383. if (S_ISREG(inode->i_mode)) {
  384. inode->i_op = &nilfs_file_inode_operations;
  385. inode->i_fop = &nilfs_file_operations;
  386. inode->i_mapping->a_ops = &nilfs_aops;
  387. } else if (S_ISDIR(inode->i_mode)) {
  388. inode->i_op = &nilfs_dir_inode_operations;
  389. inode->i_fop = &nilfs_dir_operations;
  390. inode->i_mapping->a_ops = &nilfs_aops;
  391. } else if (S_ISLNK(inode->i_mode)) {
  392. inode->i_op = &nilfs_symlink_inode_operations;
  393. inode->i_mapping->a_ops = &nilfs_aops;
  394. } else {
  395. inode->i_op = &nilfs_special_inode_operations;
  396. init_special_inode(
  397. inode, inode->i_mode,
  398. huge_decode_dev(le64_to_cpu(raw_inode->i_device_code)));
  399. }
  400. nilfs_ifile_unmap_inode(root->ifile, ino, bh);
  401. brelse(bh);
  402. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  403. nilfs_set_inode_flags(inode);
  404. return 0;
  405. failed_unmap:
  406. nilfs_ifile_unmap_inode(root->ifile, ino, bh);
  407. brelse(bh);
  408. bad_inode:
  409. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  410. return err;
  411. }
  412. static int nilfs_iget_test(struct inode *inode, void *opaque)
  413. {
  414. struct nilfs_iget_args *args = opaque;
  415. struct nilfs_inode_info *ii;
  416. if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root)
  417. return 0;
  418. ii = NILFS_I(inode);
  419. if (!test_bit(NILFS_I_GCINODE, &ii->i_state))
  420. return !args->for_gc;
  421. return args->for_gc && args->cno == ii->i_cno;
  422. }
  423. static int nilfs_iget_set(struct inode *inode, void *opaque)
  424. {
  425. struct nilfs_iget_args *args = opaque;
  426. inode->i_ino = args->ino;
  427. if (args->for_gc) {
  428. NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE;
  429. NILFS_I(inode)->i_cno = args->cno;
  430. NILFS_I(inode)->i_root = NULL;
  431. } else {
  432. if (args->root && args->ino == NILFS_ROOT_INO)
  433. nilfs_get_root(args->root);
  434. NILFS_I(inode)->i_root = args->root;
  435. }
  436. return 0;
  437. }
  438. struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root,
  439. unsigned long ino)
  440. {
  441. struct nilfs_iget_args args = {
  442. .ino = ino, .root = root, .cno = 0, .for_gc = 0
  443. };
  444. return ilookup5(sb, ino, nilfs_iget_test, &args);
  445. }
  446. struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root,
  447. unsigned long ino)
  448. {
  449. struct nilfs_iget_args args = {
  450. .ino = ino, .root = root, .cno = 0, .for_gc = 0
  451. };
  452. return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  453. }
  454. struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root,
  455. unsigned long ino)
  456. {
  457. struct inode *inode;
  458. int err;
  459. inode = nilfs_iget_locked(sb, root, ino);
  460. if (unlikely(!inode))
  461. return ERR_PTR(-ENOMEM);
  462. if (!(inode->i_state & I_NEW))
  463. return inode;
  464. err = __nilfs_read_inode(sb, root, ino, inode);
  465. if (unlikely(err)) {
  466. iget_failed(inode);
  467. return ERR_PTR(err);
  468. }
  469. unlock_new_inode(inode);
  470. return inode;
  471. }
  472. struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino,
  473. __u64 cno)
  474. {
  475. struct nilfs_iget_args args = {
  476. .ino = ino, .root = NULL, .cno = cno, .for_gc = 1
  477. };
  478. struct inode *inode;
  479. int err;
  480. inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args);
  481. if (unlikely(!inode))
  482. return ERR_PTR(-ENOMEM);
  483. if (!(inode->i_state & I_NEW))
  484. return inode;
  485. err = nilfs_init_gcinode(inode);
  486. if (unlikely(err)) {
  487. iget_failed(inode);
  488. return ERR_PTR(err);
  489. }
  490. unlock_new_inode(inode);
  491. return inode;
  492. }
  493. void nilfs_write_inode_common(struct inode *inode,
  494. struct nilfs_inode *raw_inode, int has_bmap)
  495. {
  496. struct nilfs_inode_info *ii = NILFS_I(inode);
  497. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  498. raw_inode->i_uid = cpu_to_le32(inode->i_uid);
  499. raw_inode->i_gid = cpu_to_le32(inode->i_gid);
  500. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  501. raw_inode->i_size = cpu_to_le64(inode->i_size);
  502. raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  503. raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  504. raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  505. raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  506. raw_inode->i_blocks = cpu_to_le64(inode->i_blocks);
  507. raw_inode->i_flags = cpu_to_le32(ii->i_flags);
  508. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  509. if (has_bmap)
  510. nilfs_bmap_write(ii->i_bmap, raw_inode);
  511. else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
  512. raw_inode->i_device_code =
  513. cpu_to_le64(huge_encode_dev(inode->i_rdev));
  514. /* When extending inode, nilfs->ns_inode_size should be checked
  515. for substitutions of appended fields */
  516. }
  517. void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh)
  518. {
  519. ino_t ino = inode->i_ino;
  520. struct nilfs_inode_info *ii = NILFS_I(inode);
  521. struct inode *ifile = ii->i_root->ifile;
  522. struct nilfs_inode *raw_inode;
  523. raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh);
  524. if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state))
  525. memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size);
  526. set_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
  527. nilfs_write_inode_common(inode, raw_inode, 0);
  528. /* XXX: call with has_bmap = 0 is a workaround to avoid
  529. deadlock of bmap. This delays update of i_bmap to just
  530. before writing */
  531. nilfs_ifile_unmap_inode(ifile, ino, ibh);
  532. }
  533. #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */
  534. static void nilfs_truncate_bmap(struct nilfs_inode_info *ii,
  535. unsigned long from)
  536. {
  537. unsigned long b;
  538. int ret;
  539. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  540. return;
  541. repeat:
  542. ret = nilfs_bmap_last_key(ii->i_bmap, &b);
  543. if (ret == -ENOENT)
  544. return;
  545. else if (ret < 0)
  546. goto failed;
  547. if (b < from)
  548. return;
  549. b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from);
  550. ret = nilfs_bmap_truncate(ii->i_bmap, b);
  551. nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb);
  552. if (!ret || (ret == -ENOMEM &&
  553. nilfs_bmap_truncate(ii->i_bmap, b) == 0))
  554. goto repeat;
  555. failed:
  556. nilfs_warning(ii->vfs_inode.i_sb, __func__,
  557. "failed to truncate bmap (ino=%lu, err=%d)",
  558. ii->vfs_inode.i_ino, ret);
  559. }
  560. void nilfs_truncate(struct inode *inode)
  561. {
  562. unsigned long blkoff;
  563. unsigned int blocksize;
  564. struct nilfs_transaction_info ti;
  565. struct super_block *sb = inode->i_sb;
  566. struct nilfs_inode_info *ii = NILFS_I(inode);
  567. if (!test_bit(NILFS_I_BMAP, &ii->i_state))
  568. return;
  569. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  570. return;
  571. blocksize = sb->s_blocksize;
  572. blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
  573. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  574. block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);
  575. nilfs_truncate_bmap(ii, blkoff);
  576. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  577. if (IS_SYNC(inode))
  578. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  579. nilfs_mark_inode_dirty(inode);
  580. nilfs_set_file_dirty(inode, 0);
  581. nilfs_transaction_commit(sb);
  582. /* May construct a logical segment and may fail in sync mode.
  583. But truncate has no return value. */
  584. }
  585. static void nilfs_clear_inode(struct inode *inode)
  586. {
  587. struct nilfs_inode_info *ii = NILFS_I(inode);
  588. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  589. /*
  590. * Free resources allocated in nilfs_read_inode(), here.
  591. */
  592. BUG_ON(!list_empty(&ii->i_dirty));
  593. brelse(ii->i_bh);
  594. ii->i_bh = NULL;
  595. if (mdi && mdi->mi_palloc_cache)
  596. nilfs_palloc_destroy_cache(inode);
  597. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  598. nilfs_bmap_clear(ii->i_bmap);
  599. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  600. if (ii->i_root && inode->i_ino == NILFS_ROOT_INO)
  601. nilfs_put_root(ii->i_root);
  602. }
  603. void nilfs_evict_inode(struct inode *inode)
  604. {
  605. struct nilfs_transaction_info ti;
  606. struct super_block *sb = inode->i_sb;
  607. struct nilfs_inode_info *ii = NILFS_I(inode);
  608. if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) {
  609. if (inode->i_data.nrpages)
  610. truncate_inode_pages(&inode->i_data, 0);
  611. end_writeback(inode);
  612. nilfs_clear_inode(inode);
  613. return;
  614. }
  615. nilfs_transaction_begin(sb, &ti, 0); /* never fails */
  616. if (inode->i_data.nrpages)
  617. truncate_inode_pages(&inode->i_data, 0);
  618. /* TODO: some of the following operations may fail. */
  619. nilfs_truncate_bmap(ii, 0);
  620. nilfs_mark_inode_dirty(inode);
  621. end_writeback(inode);
  622. nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino);
  623. atomic_dec(&ii->i_root->inodes_count);
  624. nilfs_clear_inode(inode);
  625. if (IS_SYNC(inode))
  626. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  627. nilfs_transaction_commit(sb);
  628. /* May construct a logical segment and may fail in sync mode.
  629. But delete_inode has no return value. */
  630. }
  631. int nilfs_setattr(struct dentry *dentry, struct iattr *iattr)
  632. {
  633. struct nilfs_transaction_info ti;
  634. struct inode *inode = dentry->d_inode;
  635. struct super_block *sb = inode->i_sb;
  636. int err;
  637. err = inode_change_ok(inode, iattr);
  638. if (err)
  639. return err;
  640. err = nilfs_transaction_begin(sb, &ti, 0);
  641. if (unlikely(err))
  642. return err;
  643. if ((iattr->ia_valid & ATTR_SIZE) &&
  644. iattr->ia_size != i_size_read(inode)) {
  645. err = vmtruncate(inode, iattr->ia_size);
  646. if (unlikely(err))
  647. goto out_err;
  648. }
  649. setattr_copy(inode, iattr);
  650. mark_inode_dirty(inode);
  651. if (iattr->ia_valid & ATTR_MODE) {
  652. err = nilfs_acl_chmod(inode);
  653. if (unlikely(err))
  654. goto out_err;
  655. }
  656. return nilfs_transaction_commit(sb);
  657. out_err:
  658. nilfs_transaction_abort(sb);
  659. return err;
  660. }
  661. int nilfs_permission(struct inode *inode, int mask, unsigned int flags)
  662. {
  663. struct nilfs_root *root;
  664. if (flags & IPERM_FLAG_RCU)
  665. return -ECHILD;
  666. root = NILFS_I(inode)->i_root;
  667. if ((mask & MAY_WRITE) && root &&
  668. root->cno != NILFS_CPTREE_CURRENT_CNO)
  669. return -EROFS; /* snapshot is not writable */
  670. return generic_permission(inode, mask, flags, NULL);
  671. }
  672. int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
  673. {
  674. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  675. struct nilfs_inode_info *ii = NILFS_I(inode);
  676. int err;
  677. spin_lock(&sbi->s_inode_lock);
  678. if (ii->i_bh == NULL) {
  679. spin_unlock(&sbi->s_inode_lock);
  680. err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
  681. inode->i_ino, pbh);
  682. if (unlikely(err))
  683. return err;
  684. spin_lock(&sbi->s_inode_lock);
  685. if (ii->i_bh == NULL)
  686. ii->i_bh = *pbh;
  687. else {
  688. brelse(*pbh);
  689. *pbh = ii->i_bh;
  690. }
  691. } else
  692. *pbh = ii->i_bh;
  693. get_bh(*pbh);
  694. spin_unlock(&sbi->s_inode_lock);
  695. return 0;
  696. }
  697. int nilfs_inode_dirty(struct inode *inode)
  698. {
  699. struct nilfs_inode_info *ii = NILFS_I(inode);
  700. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  701. int ret = 0;
  702. if (!list_empty(&ii->i_dirty)) {
  703. spin_lock(&sbi->s_inode_lock);
  704. ret = test_bit(NILFS_I_DIRTY, &ii->i_state) ||
  705. test_bit(NILFS_I_BUSY, &ii->i_state);
  706. spin_unlock(&sbi->s_inode_lock);
  707. }
  708. return ret;
  709. }
  710. int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty)
  711. {
  712. struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
  713. struct nilfs_inode_info *ii = NILFS_I(inode);
  714. atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks);
  715. if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state))
  716. return 0;
  717. spin_lock(&sbi->s_inode_lock);
  718. if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
  719. !test_bit(NILFS_I_BUSY, &ii->i_state)) {
  720. /* Because this routine may race with nilfs_dispose_list(),
  721. we have to check NILFS_I_QUEUED here, too. */
  722. if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) {
  723. /* This will happen when somebody is freeing
  724. this inode. */
  725. nilfs_warning(sbi->s_super, __func__,
  726. "cannot get inode (ino=%lu)\n",
  727. inode->i_ino);
  728. spin_unlock(&sbi->s_inode_lock);
  729. return -EINVAL; /* NILFS_I_DIRTY may remain for
  730. freeing inode */
  731. }
  732. list_del(&ii->i_dirty);
  733. list_add_tail(&ii->i_dirty, &sbi->s_dirty_files);
  734. set_bit(NILFS_I_QUEUED, &ii->i_state);
  735. }
  736. spin_unlock(&sbi->s_inode_lock);
  737. return 0;
  738. }
  739. int nilfs_mark_inode_dirty(struct inode *inode)
  740. {
  741. struct buffer_head *ibh;
  742. int err;
  743. err = nilfs_load_inode_block(inode, &ibh);
  744. if (unlikely(err)) {
  745. nilfs_warning(inode->i_sb, __func__,
  746. "failed to reget inode block.\n");
  747. return err;
  748. }
  749. nilfs_update_inode(inode, ibh);
  750. nilfs_mdt_mark_buffer_dirty(ibh);
  751. nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile);
  752. brelse(ibh);
  753. return 0;
  754. }
  755. /**
  756. * nilfs_dirty_inode - reflect changes on given inode to an inode block.
  757. * @inode: inode of the file to be registered.
  758. *
  759. * nilfs_dirty_inode() loads a inode block containing the specified
  760. * @inode and copies data from a nilfs_inode to a corresponding inode
  761. * entry in the inode block. This operation is excluded from the segment
  762. * construction. This function can be called both as a single operation
  763. * and as a part of indivisible file operations.
  764. */
  765. void nilfs_dirty_inode(struct inode *inode)
  766. {
  767. struct nilfs_transaction_info ti;
  768. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  769. if (is_bad_inode(inode)) {
  770. nilfs_warning(inode->i_sb, __func__,
  771. "tried to mark bad_inode dirty. ignored.\n");
  772. dump_stack();
  773. return;
  774. }
  775. if (mdi) {
  776. nilfs_mdt_mark_dirty(inode);
  777. return;
  778. }
  779. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  780. nilfs_mark_inode_dirty(inode);
  781. nilfs_transaction_commit(inode->i_sb); /* never fails */
  782. }
  783. int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  784. __u64 start, __u64 len)
  785. {
  786. struct the_nilfs *nilfs = NILFS_I_NILFS(inode);
  787. __u64 logical = 0, phys = 0, size = 0;
  788. __u32 flags = 0;
  789. loff_t isize;
  790. sector_t blkoff, end_blkoff;
  791. sector_t delalloc_blkoff;
  792. unsigned long delalloc_blklen;
  793. unsigned int blkbits = inode->i_blkbits;
  794. int ret, n;
  795. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  796. if (ret)
  797. return ret;
  798. mutex_lock(&inode->i_mutex);
  799. isize = i_size_read(inode);
  800. blkoff = start >> blkbits;
  801. end_blkoff = (start + len - 1) >> blkbits;
  802. delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff,
  803. &delalloc_blkoff);
  804. do {
  805. __u64 blkphy;
  806. unsigned int maxblocks;
  807. if (delalloc_blklen && blkoff == delalloc_blkoff) {
  808. if (size) {
  809. /* End of the current extent */
  810. ret = fiemap_fill_next_extent(
  811. fieinfo, logical, phys, size, flags);
  812. if (ret)
  813. break;
  814. }
  815. if (blkoff > end_blkoff)
  816. break;
  817. flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC;
  818. logical = blkoff << blkbits;
  819. phys = 0;
  820. size = delalloc_blklen << blkbits;
  821. blkoff = delalloc_blkoff + delalloc_blklen;
  822. delalloc_blklen = nilfs_find_uncommitted_extent(
  823. inode, blkoff, &delalloc_blkoff);
  824. continue;
  825. }
  826. /*
  827. * Limit the number of blocks that we look up so as
  828. * not to get into the next delayed allocation extent.
  829. */
  830. maxblocks = INT_MAX;
  831. if (delalloc_blklen)
  832. maxblocks = min_t(sector_t, delalloc_blkoff - blkoff,
  833. maxblocks);
  834. blkphy = 0;
  835. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  836. n = nilfs_bmap_lookup_contig(
  837. NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks);
  838. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  839. if (n < 0) {
  840. int past_eof;
  841. if (unlikely(n != -ENOENT))
  842. break; /* error */
  843. /* HOLE */
  844. blkoff++;
  845. past_eof = ((blkoff << blkbits) >= isize);
  846. if (size) {
  847. /* End of the current extent */
  848. if (past_eof)
  849. flags |= FIEMAP_EXTENT_LAST;
  850. ret = fiemap_fill_next_extent(
  851. fieinfo, logical, phys, size, flags);
  852. if (ret)
  853. break;
  854. size = 0;
  855. }
  856. if (blkoff > end_blkoff || past_eof)
  857. break;
  858. } else {
  859. if (size) {
  860. if (phys && blkphy << blkbits == phys + size) {
  861. /* The current extent goes on */
  862. size += n << blkbits;
  863. } else {
  864. /* Terminate the current extent */
  865. ret = fiemap_fill_next_extent(
  866. fieinfo, logical, phys, size,
  867. flags);
  868. if (ret || blkoff > end_blkoff)
  869. break;
  870. /* Start another extent */
  871. flags = FIEMAP_EXTENT_MERGED;
  872. logical = blkoff << blkbits;
  873. phys = blkphy << blkbits;
  874. size = n << blkbits;
  875. }
  876. } else {
  877. /* Start a new extent */
  878. flags = FIEMAP_EXTENT_MERGED;
  879. logical = blkoff << blkbits;
  880. phys = blkphy << blkbits;
  881. size = n << blkbits;
  882. }
  883. blkoff += n;
  884. }
  885. cond_resched();
  886. } while (true);
  887. /* If ret is 1 then we just hit the end of the extent array */
  888. if (ret == 1)
  889. ret = 0;
  890. mutex_unlock(&inode->i_mutex);
  891. return ret;
  892. }