inode.c 29 KB

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