inode.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * (C) 1997 Linus Torvalds
  3. * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/mm.h>
  7. #include <linux/dcache.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/writeback.h>
  11. #include <linux/module.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/wait.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/hash.h>
  16. #include <linux/swap.h>
  17. #include <linux/security.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/cdev.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/mount.h>
  23. #include <linux/async.h>
  24. #include <linux/posix_acl.h>
  25. #include <linux/prefetch.h>
  26. #include <linux/ima.h>
  27. #include <linux/cred.h>
  28. #include <linux/buffer_head.h> /* for inode_has_buffers */
  29. #include <linux/ratelimit.h>
  30. #include "internal.h"
  31. /*
  32. * Inode locking rules:
  33. *
  34. * inode->i_lock protects:
  35. * inode->i_state, inode->i_hash, __iget()
  36. * inode->i_sb->s_inode_lru_lock protects:
  37. * inode->i_sb->s_inode_lru, inode->i_lru
  38. * inode_sb_list_lock protects:
  39. * sb->s_inodes, inode->i_sb_list
  40. * bdi->wb.list_lock protects:
  41. * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
  42. * inode_hash_lock protects:
  43. * inode_hashtable, inode->i_hash
  44. *
  45. * Lock ordering:
  46. *
  47. * inode_sb_list_lock
  48. * inode->i_lock
  49. * inode->i_sb->s_inode_lru_lock
  50. *
  51. * bdi->wb.list_lock
  52. * inode->i_lock
  53. *
  54. * inode_hash_lock
  55. * inode_sb_list_lock
  56. * inode->i_lock
  57. *
  58. * iunique_lock
  59. * inode_hash_lock
  60. */
  61. static unsigned int i_hash_mask __read_mostly;
  62. static unsigned int i_hash_shift __read_mostly;
  63. static struct hlist_head *inode_hashtable __read_mostly;
  64. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
  65. __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
  66. /*
  67. * Empty aops. Can be used for the cases where the user does not
  68. * define any of the address_space operations.
  69. */
  70. const struct address_space_operations empty_aops = {
  71. };
  72. EXPORT_SYMBOL(empty_aops);
  73. /*
  74. * Statistics gathering..
  75. */
  76. struct inodes_stat_t inodes_stat;
  77. static DEFINE_PER_CPU(unsigned int, nr_inodes);
  78. static DEFINE_PER_CPU(unsigned int, nr_unused);
  79. static struct kmem_cache *inode_cachep __read_mostly;
  80. static int get_nr_inodes(void)
  81. {
  82. int i;
  83. int sum = 0;
  84. for_each_possible_cpu(i)
  85. sum += per_cpu(nr_inodes, i);
  86. return sum < 0 ? 0 : sum;
  87. }
  88. static inline int get_nr_inodes_unused(void)
  89. {
  90. int i;
  91. int sum = 0;
  92. for_each_possible_cpu(i)
  93. sum += per_cpu(nr_unused, i);
  94. return sum < 0 ? 0 : sum;
  95. }
  96. int get_nr_dirty_inodes(void)
  97. {
  98. /* not actually dirty inodes, but a wild approximation */
  99. int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
  100. return nr_dirty > 0 ? nr_dirty : 0;
  101. }
  102. /*
  103. * Handle nr_inode sysctl
  104. */
  105. #ifdef CONFIG_SYSCTL
  106. int proc_nr_inodes(ctl_table *table, int write,
  107. void __user *buffer, size_t *lenp, loff_t *ppos)
  108. {
  109. inodes_stat.nr_inodes = get_nr_inodes();
  110. inodes_stat.nr_unused = get_nr_inodes_unused();
  111. return proc_dointvec(table, write, buffer, lenp, ppos);
  112. }
  113. #endif
  114. /**
  115. * inode_init_always - perform inode structure intialisation
  116. * @sb: superblock inode belongs to
  117. * @inode: inode to initialise
  118. *
  119. * These are initializations that need to be done on every inode
  120. * allocation as the fields are not initialised by slab allocation.
  121. */
  122. int inode_init_always(struct super_block *sb, struct inode *inode)
  123. {
  124. static const struct inode_operations empty_iops;
  125. static const struct file_operations empty_fops;
  126. struct address_space *const mapping = &inode->i_data;
  127. inode->i_sb = sb;
  128. inode->i_blkbits = sb->s_blocksize_bits;
  129. inode->i_flags = 0;
  130. atomic_set(&inode->i_count, 1);
  131. inode->i_op = &empty_iops;
  132. inode->i_fop = &empty_fops;
  133. inode->__i_nlink = 1;
  134. inode->i_opflags = 0;
  135. inode->i_uid = 0;
  136. inode->i_gid = 0;
  137. atomic_set(&inode->i_writecount, 0);
  138. inode->i_size = 0;
  139. inode->i_blocks = 0;
  140. inode->i_bytes = 0;
  141. inode->i_generation = 0;
  142. #ifdef CONFIG_QUOTA
  143. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  144. #endif
  145. inode->i_pipe = NULL;
  146. inode->i_bdev = NULL;
  147. inode->i_cdev = NULL;
  148. inode->i_rdev = 0;
  149. inode->dirtied_when = 0;
  150. if (security_inode_alloc(inode))
  151. goto out;
  152. spin_lock_init(&inode->i_lock);
  153. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  154. mutex_init(&inode->i_mutex);
  155. lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
  156. atomic_set(&inode->i_dio_count, 0);
  157. mapping->a_ops = &empty_aops;
  158. mapping->host = inode;
  159. mapping->flags = 0;
  160. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  161. mapping->assoc_mapping = NULL;
  162. mapping->backing_dev_info = &default_backing_dev_info;
  163. mapping->writeback_index = 0;
  164. /*
  165. * If the block_device provides a backing_dev_info for client
  166. * inodes then use that. Otherwise the inode share the bdev's
  167. * backing_dev_info.
  168. */
  169. if (sb->s_bdev) {
  170. struct backing_dev_info *bdi;
  171. bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  172. mapping->backing_dev_info = bdi;
  173. }
  174. inode->i_private = NULL;
  175. inode->i_mapping = mapping;
  176. INIT_LIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
  177. #ifdef CONFIG_FS_POSIX_ACL
  178. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  179. #endif
  180. #ifdef CONFIG_FSNOTIFY
  181. inode->i_fsnotify_mask = 0;
  182. #endif
  183. this_cpu_inc(nr_inodes);
  184. return 0;
  185. out:
  186. return -ENOMEM;
  187. }
  188. EXPORT_SYMBOL(inode_init_always);
  189. static struct inode *alloc_inode(struct super_block *sb)
  190. {
  191. struct inode *inode;
  192. if (sb->s_op->alloc_inode)
  193. inode = sb->s_op->alloc_inode(sb);
  194. else
  195. inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
  196. if (!inode)
  197. return NULL;
  198. if (unlikely(inode_init_always(sb, inode))) {
  199. if (inode->i_sb->s_op->destroy_inode)
  200. inode->i_sb->s_op->destroy_inode(inode);
  201. else
  202. kmem_cache_free(inode_cachep, inode);
  203. return NULL;
  204. }
  205. return inode;
  206. }
  207. void free_inode_nonrcu(struct inode *inode)
  208. {
  209. kmem_cache_free(inode_cachep, inode);
  210. }
  211. EXPORT_SYMBOL(free_inode_nonrcu);
  212. void __destroy_inode(struct inode *inode)
  213. {
  214. BUG_ON(inode_has_buffers(inode));
  215. security_inode_free(inode);
  216. fsnotify_inode_delete(inode);
  217. if (!inode->i_nlink) {
  218. WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
  219. atomic_long_dec(&inode->i_sb->s_remove_count);
  220. }
  221. #ifdef CONFIG_FS_POSIX_ACL
  222. if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
  223. posix_acl_release(inode->i_acl);
  224. if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
  225. posix_acl_release(inode->i_default_acl);
  226. #endif
  227. this_cpu_dec(nr_inodes);
  228. }
  229. EXPORT_SYMBOL(__destroy_inode);
  230. static void i_callback(struct rcu_head *head)
  231. {
  232. struct inode *inode = container_of(head, struct inode, i_rcu);
  233. kmem_cache_free(inode_cachep, inode);
  234. }
  235. static void destroy_inode(struct inode *inode)
  236. {
  237. BUG_ON(!list_empty(&inode->i_lru));
  238. __destroy_inode(inode);
  239. if (inode->i_sb->s_op->destroy_inode)
  240. inode->i_sb->s_op->destroy_inode(inode);
  241. else
  242. call_rcu(&inode->i_rcu, i_callback);
  243. }
  244. /**
  245. * drop_nlink - directly drop an inode's link count
  246. * @inode: inode
  247. *
  248. * This is a low-level filesystem helper to replace any
  249. * direct filesystem manipulation of i_nlink. In cases
  250. * where we are attempting to track writes to the
  251. * filesystem, a decrement to zero means an imminent
  252. * write when the file is truncated and actually unlinked
  253. * on the filesystem.
  254. */
  255. void drop_nlink(struct inode *inode)
  256. {
  257. WARN_ON(inode->i_nlink == 0);
  258. inode->__i_nlink--;
  259. if (!inode->i_nlink)
  260. atomic_long_inc(&inode->i_sb->s_remove_count);
  261. }
  262. EXPORT_SYMBOL(drop_nlink);
  263. /**
  264. * clear_nlink - directly zero an inode's link count
  265. * @inode: inode
  266. *
  267. * This is a low-level filesystem helper to replace any
  268. * direct filesystem manipulation of i_nlink. See
  269. * drop_nlink() for why we care about i_nlink hitting zero.
  270. */
  271. void clear_nlink(struct inode *inode)
  272. {
  273. if (inode->i_nlink) {
  274. inode->__i_nlink = 0;
  275. atomic_long_inc(&inode->i_sb->s_remove_count);
  276. }
  277. }
  278. EXPORT_SYMBOL(clear_nlink);
  279. /**
  280. * set_nlink - directly set an inode's link count
  281. * @inode: inode
  282. * @nlink: new nlink (should be non-zero)
  283. *
  284. * This is a low-level filesystem helper to replace any
  285. * direct filesystem manipulation of i_nlink.
  286. */
  287. void set_nlink(struct inode *inode, unsigned int nlink)
  288. {
  289. if (!nlink) {
  290. printk_ratelimited(KERN_INFO
  291. "set_nlink() clearing i_nlink on %s inode %li\n",
  292. inode->i_sb->s_type->name, inode->i_ino);
  293. clear_nlink(inode);
  294. } else {
  295. /* Yes, some filesystems do change nlink from zero to one */
  296. if (inode->i_nlink == 0)
  297. atomic_long_dec(&inode->i_sb->s_remove_count);
  298. inode->__i_nlink = nlink;
  299. }
  300. }
  301. EXPORT_SYMBOL(set_nlink);
  302. /**
  303. * inc_nlink - directly increment an inode's link count
  304. * @inode: inode
  305. *
  306. * This is a low-level filesystem helper to replace any
  307. * direct filesystem manipulation of i_nlink. Currently,
  308. * it is only here for parity with dec_nlink().
  309. */
  310. void inc_nlink(struct inode *inode)
  311. {
  312. if (WARN_ON(inode->i_nlink == 0))
  313. atomic_long_dec(&inode->i_sb->s_remove_count);
  314. inode->__i_nlink++;
  315. }
  316. EXPORT_SYMBOL(inc_nlink);
  317. void address_space_init_once(struct address_space *mapping)
  318. {
  319. memset(mapping, 0, sizeof(*mapping));
  320. INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
  321. spin_lock_init(&mapping->tree_lock);
  322. mutex_init(&mapping->i_mmap_mutex);
  323. INIT_LIST_HEAD(&mapping->private_list);
  324. spin_lock_init(&mapping->private_lock);
  325. INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
  326. INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
  327. }
  328. EXPORT_SYMBOL(address_space_init_once);
  329. /*
  330. * These are initializations that only need to be done
  331. * once, because the fields are idempotent across use
  332. * of the inode, so let the slab aware of that.
  333. */
  334. void inode_init_once(struct inode *inode)
  335. {
  336. memset(inode, 0, sizeof(*inode));
  337. INIT_HLIST_NODE(&inode->i_hash);
  338. INIT_LIST_HEAD(&inode->i_devices);
  339. INIT_LIST_HEAD(&inode->i_wb_list);
  340. INIT_LIST_HEAD(&inode->i_lru);
  341. address_space_init_once(&inode->i_data);
  342. i_size_ordered_init(inode);
  343. #ifdef CONFIG_FSNOTIFY
  344. INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
  345. #endif
  346. }
  347. EXPORT_SYMBOL(inode_init_once);
  348. static void init_once(void *foo)
  349. {
  350. struct inode *inode = (struct inode *) foo;
  351. inode_init_once(inode);
  352. }
  353. /*
  354. * inode->i_lock must be held
  355. */
  356. void __iget(struct inode *inode)
  357. {
  358. atomic_inc(&inode->i_count);
  359. }
  360. /*
  361. * get additional reference to inode; caller must already hold one.
  362. */
  363. void ihold(struct inode *inode)
  364. {
  365. WARN_ON(atomic_inc_return(&inode->i_count) < 2);
  366. }
  367. EXPORT_SYMBOL(ihold);
  368. static void inode_lru_list_add(struct inode *inode)
  369. {
  370. spin_lock(&inode->i_sb->s_inode_lru_lock);
  371. if (list_empty(&inode->i_lru)) {
  372. list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
  373. inode->i_sb->s_nr_inodes_unused++;
  374. this_cpu_inc(nr_unused);
  375. }
  376. spin_unlock(&inode->i_sb->s_inode_lru_lock);
  377. }
  378. static void inode_lru_list_del(struct inode *inode)
  379. {
  380. spin_lock(&inode->i_sb->s_inode_lru_lock);
  381. if (!list_empty(&inode->i_lru)) {
  382. list_del_init(&inode->i_lru);
  383. inode->i_sb->s_nr_inodes_unused--;
  384. this_cpu_dec(nr_unused);
  385. }
  386. spin_unlock(&inode->i_sb->s_inode_lru_lock);
  387. }
  388. /**
  389. * inode_sb_list_add - add inode to the superblock list of inodes
  390. * @inode: inode to add
  391. */
  392. void inode_sb_list_add(struct inode *inode)
  393. {
  394. spin_lock(&inode_sb_list_lock);
  395. list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
  396. spin_unlock(&inode_sb_list_lock);
  397. }
  398. EXPORT_SYMBOL_GPL(inode_sb_list_add);
  399. static inline void inode_sb_list_del(struct inode *inode)
  400. {
  401. if (!list_empty(&inode->i_sb_list)) {
  402. spin_lock(&inode_sb_list_lock);
  403. list_del_init(&inode->i_sb_list);
  404. spin_unlock(&inode_sb_list_lock);
  405. }
  406. }
  407. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  408. {
  409. unsigned long tmp;
  410. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  411. L1_CACHE_BYTES;
  412. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
  413. return tmp & i_hash_mask;
  414. }
  415. /**
  416. * __insert_inode_hash - hash an inode
  417. * @inode: unhashed inode
  418. * @hashval: unsigned long value used to locate this object in the
  419. * inode_hashtable.
  420. *
  421. * Add an inode to the inode hash for this superblock.
  422. */
  423. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  424. {
  425. struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
  426. spin_lock(&inode_hash_lock);
  427. spin_lock(&inode->i_lock);
  428. hlist_add_head(&inode->i_hash, b);
  429. spin_unlock(&inode->i_lock);
  430. spin_unlock(&inode_hash_lock);
  431. }
  432. EXPORT_SYMBOL(__insert_inode_hash);
  433. /**
  434. * __remove_inode_hash - remove an inode from the hash
  435. * @inode: inode to unhash
  436. *
  437. * Remove an inode from the superblock.
  438. */
  439. void __remove_inode_hash(struct inode *inode)
  440. {
  441. spin_lock(&inode_hash_lock);
  442. spin_lock(&inode->i_lock);
  443. hlist_del_init(&inode->i_hash);
  444. spin_unlock(&inode->i_lock);
  445. spin_unlock(&inode_hash_lock);
  446. }
  447. EXPORT_SYMBOL(__remove_inode_hash);
  448. void end_writeback(struct inode *inode)
  449. {
  450. might_sleep();
  451. /*
  452. * We have to cycle tree_lock here because reclaim can be still in the
  453. * process of removing the last page (in __delete_from_page_cache())
  454. * and we must not free mapping under it.
  455. */
  456. spin_lock_irq(&inode->i_data.tree_lock);
  457. BUG_ON(inode->i_data.nrpages);
  458. spin_unlock_irq(&inode->i_data.tree_lock);
  459. BUG_ON(!list_empty(&inode->i_data.private_list));
  460. BUG_ON(!(inode->i_state & I_FREEING));
  461. BUG_ON(inode->i_state & I_CLEAR);
  462. inode_sync_wait(inode);
  463. /* don't need i_lock here, no concurrent mods to i_state */
  464. inode->i_state = I_FREEING | I_CLEAR;
  465. }
  466. EXPORT_SYMBOL(end_writeback);
  467. /*
  468. * Free the inode passed in, removing it from the lists it is still connected
  469. * to. We remove any pages still attached to the inode and wait for any IO that
  470. * is still in progress before finally destroying the inode.
  471. *
  472. * An inode must already be marked I_FREEING so that we avoid the inode being
  473. * moved back onto lists if we race with other code that manipulates the lists
  474. * (e.g. writeback_single_inode). The caller is responsible for setting this.
  475. *
  476. * An inode must already be removed from the LRU list before being evicted from
  477. * the cache. This should occur atomically with setting the I_FREEING state
  478. * flag, so no inodes here should ever be on the LRU when being evicted.
  479. */
  480. static void evict(struct inode *inode)
  481. {
  482. const struct super_operations *op = inode->i_sb->s_op;
  483. BUG_ON(!(inode->i_state & I_FREEING));
  484. BUG_ON(!list_empty(&inode->i_lru));
  485. if (!list_empty(&inode->i_wb_list))
  486. inode_wb_list_del(inode);
  487. inode_sb_list_del(inode);
  488. if (op->evict_inode) {
  489. op->evict_inode(inode);
  490. } else {
  491. if (inode->i_data.nrpages)
  492. truncate_inode_pages(&inode->i_data, 0);
  493. end_writeback(inode);
  494. }
  495. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  496. bd_forget(inode);
  497. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  498. cd_forget(inode);
  499. remove_inode_hash(inode);
  500. spin_lock(&inode->i_lock);
  501. wake_up_bit(&inode->i_state, __I_NEW);
  502. BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
  503. spin_unlock(&inode->i_lock);
  504. destroy_inode(inode);
  505. }
  506. /*
  507. * dispose_list - dispose of the contents of a local list
  508. * @head: the head of the list to free
  509. *
  510. * Dispose-list gets a local list with local inodes in it, so it doesn't
  511. * need to worry about list corruption and SMP locks.
  512. */
  513. static void dispose_list(struct list_head *head)
  514. {
  515. while (!list_empty(head)) {
  516. struct inode *inode;
  517. inode = list_first_entry(head, struct inode, i_lru);
  518. list_del_init(&inode->i_lru);
  519. evict(inode);
  520. }
  521. }
  522. /**
  523. * evict_inodes - evict all evictable inodes for a superblock
  524. * @sb: superblock to operate on
  525. *
  526. * Make sure that no inodes with zero refcount are retained. This is
  527. * called by superblock shutdown after having MS_ACTIVE flag removed,
  528. * so any inode reaching zero refcount during or after that call will
  529. * be immediately evicted.
  530. */
  531. void evict_inodes(struct super_block *sb)
  532. {
  533. struct inode *inode, *next;
  534. LIST_HEAD(dispose);
  535. spin_lock(&inode_sb_list_lock);
  536. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  537. if (atomic_read(&inode->i_count))
  538. continue;
  539. spin_lock(&inode->i_lock);
  540. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  541. spin_unlock(&inode->i_lock);
  542. continue;
  543. }
  544. inode->i_state |= I_FREEING;
  545. inode_lru_list_del(inode);
  546. spin_unlock(&inode->i_lock);
  547. list_add(&inode->i_lru, &dispose);
  548. }
  549. spin_unlock(&inode_sb_list_lock);
  550. dispose_list(&dispose);
  551. }
  552. /**
  553. * invalidate_inodes - attempt to free all inodes on a superblock
  554. * @sb: superblock to operate on
  555. * @kill_dirty: flag to guide handling of dirty inodes
  556. *
  557. * Attempts to free all inodes for a given superblock. If there were any
  558. * busy inodes return a non-zero value, else zero.
  559. * If @kill_dirty is set, discard dirty inodes too, otherwise treat
  560. * them as busy.
  561. */
  562. int invalidate_inodes(struct super_block *sb, bool kill_dirty)
  563. {
  564. int busy = 0;
  565. struct inode *inode, *next;
  566. LIST_HEAD(dispose);
  567. spin_lock(&inode_sb_list_lock);
  568. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  569. spin_lock(&inode->i_lock);
  570. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  571. spin_unlock(&inode->i_lock);
  572. continue;
  573. }
  574. if (inode->i_state & I_DIRTY && !kill_dirty) {
  575. spin_unlock(&inode->i_lock);
  576. busy = 1;
  577. continue;
  578. }
  579. if (atomic_read(&inode->i_count)) {
  580. spin_unlock(&inode->i_lock);
  581. busy = 1;
  582. continue;
  583. }
  584. inode->i_state |= I_FREEING;
  585. inode_lru_list_del(inode);
  586. spin_unlock(&inode->i_lock);
  587. list_add(&inode->i_lru, &dispose);
  588. }
  589. spin_unlock(&inode_sb_list_lock);
  590. dispose_list(&dispose);
  591. return busy;
  592. }
  593. static int can_unuse(struct inode *inode)
  594. {
  595. if (inode->i_state & ~I_REFERENCED)
  596. return 0;
  597. if (inode_has_buffers(inode))
  598. return 0;
  599. if (atomic_read(&inode->i_count))
  600. return 0;
  601. if (inode->i_data.nrpages)
  602. return 0;
  603. return 1;
  604. }
  605. /*
  606. * Walk the superblock inode LRU for freeable inodes and attempt to free them.
  607. * This is called from the superblock shrinker function with a number of inodes
  608. * to trim from the LRU. Inodes to be freed are moved to a temporary list and
  609. * then are freed outside inode_lock by dispose_list().
  610. *
  611. * Any inodes which are pinned purely because of attached pagecache have their
  612. * pagecache removed. If the inode has metadata buffers attached to
  613. * mapping->private_list then try to remove them.
  614. *
  615. * If the inode has the I_REFERENCED flag set, then it means that it has been
  616. * used recently - the flag is set in iput_final(). When we encounter such an
  617. * inode, clear the flag and move it to the back of the LRU so it gets another
  618. * pass through the LRU before it gets reclaimed. This is necessary because of
  619. * the fact we are doing lazy LRU updates to minimise lock contention so the
  620. * LRU does not have strict ordering. Hence we don't want to reclaim inodes
  621. * with this flag set because they are the inodes that are out of order.
  622. */
  623. void prune_icache_sb(struct super_block *sb, int nr_to_scan)
  624. {
  625. LIST_HEAD(freeable);
  626. int nr_scanned;
  627. unsigned long reap = 0;
  628. spin_lock(&sb->s_inode_lru_lock);
  629. for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
  630. struct inode *inode;
  631. if (list_empty(&sb->s_inode_lru))
  632. break;
  633. inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
  634. /*
  635. * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
  636. * so use a trylock. If we fail to get the lock, just move the
  637. * inode to the back of the list so we don't spin on it.
  638. */
  639. if (!spin_trylock(&inode->i_lock)) {
  640. list_move_tail(&inode->i_lru, &sb->s_inode_lru);
  641. continue;
  642. }
  643. /*
  644. * Referenced or dirty inodes are still in use. Give them
  645. * another pass through the LRU as we canot reclaim them now.
  646. */
  647. if (atomic_read(&inode->i_count) ||
  648. (inode->i_state & ~I_REFERENCED)) {
  649. list_del_init(&inode->i_lru);
  650. spin_unlock(&inode->i_lock);
  651. sb->s_nr_inodes_unused--;
  652. this_cpu_dec(nr_unused);
  653. continue;
  654. }
  655. /* recently referenced inodes get one more pass */
  656. if (inode->i_state & I_REFERENCED) {
  657. inode->i_state &= ~I_REFERENCED;
  658. list_move(&inode->i_lru, &sb->s_inode_lru);
  659. spin_unlock(&inode->i_lock);
  660. continue;
  661. }
  662. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  663. __iget(inode);
  664. spin_unlock(&inode->i_lock);
  665. spin_unlock(&sb->s_inode_lru_lock);
  666. if (remove_inode_buffers(inode))
  667. reap += invalidate_mapping_pages(&inode->i_data,
  668. 0, -1);
  669. iput(inode);
  670. spin_lock(&sb->s_inode_lru_lock);
  671. if (inode != list_entry(sb->s_inode_lru.next,
  672. struct inode, i_lru))
  673. continue; /* wrong inode or list_empty */
  674. /* avoid lock inversions with trylock */
  675. if (!spin_trylock(&inode->i_lock))
  676. continue;
  677. if (!can_unuse(inode)) {
  678. spin_unlock(&inode->i_lock);
  679. continue;
  680. }
  681. }
  682. WARN_ON(inode->i_state & I_NEW);
  683. inode->i_state |= I_FREEING;
  684. spin_unlock(&inode->i_lock);
  685. list_move(&inode->i_lru, &freeable);
  686. sb->s_nr_inodes_unused--;
  687. this_cpu_dec(nr_unused);
  688. }
  689. if (current_is_kswapd())
  690. __count_vm_events(KSWAPD_INODESTEAL, reap);
  691. else
  692. __count_vm_events(PGINODESTEAL, reap);
  693. spin_unlock(&sb->s_inode_lru_lock);
  694. if (current->reclaim_state)
  695. current->reclaim_state->reclaimed_slab += reap;
  696. dispose_list(&freeable);
  697. }
  698. static void __wait_on_freeing_inode(struct inode *inode);
  699. /*
  700. * Called with the inode lock held.
  701. */
  702. static struct inode *find_inode(struct super_block *sb,
  703. struct hlist_head *head,
  704. int (*test)(struct inode *, void *),
  705. void *data)
  706. {
  707. struct hlist_node *node;
  708. struct inode *inode = NULL;
  709. repeat:
  710. hlist_for_each_entry(inode, node, head, i_hash) {
  711. spin_lock(&inode->i_lock);
  712. if (inode->i_sb != sb) {
  713. spin_unlock(&inode->i_lock);
  714. continue;
  715. }
  716. if (!test(inode, data)) {
  717. spin_unlock(&inode->i_lock);
  718. continue;
  719. }
  720. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  721. __wait_on_freeing_inode(inode);
  722. goto repeat;
  723. }
  724. __iget(inode);
  725. spin_unlock(&inode->i_lock);
  726. return inode;
  727. }
  728. return NULL;
  729. }
  730. /*
  731. * find_inode_fast is the fast path version of find_inode, see the comment at
  732. * iget_locked for details.
  733. */
  734. static struct inode *find_inode_fast(struct super_block *sb,
  735. struct hlist_head *head, unsigned long ino)
  736. {
  737. struct hlist_node *node;
  738. struct inode *inode = NULL;
  739. repeat:
  740. hlist_for_each_entry(inode, node, head, i_hash) {
  741. spin_lock(&inode->i_lock);
  742. if (inode->i_ino != ino) {
  743. spin_unlock(&inode->i_lock);
  744. continue;
  745. }
  746. if (inode->i_sb != sb) {
  747. spin_unlock(&inode->i_lock);
  748. continue;
  749. }
  750. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  751. __wait_on_freeing_inode(inode);
  752. goto repeat;
  753. }
  754. __iget(inode);
  755. spin_unlock(&inode->i_lock);
  756. return inode;
  757. }
  758. return NULL;
  759. }
  760. /*
  761. * Each cpu owns a range of LAST_INO_BATCH numbers.
  762. * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
  763. * to renew the exhausted range.
  764. *
  765. * This does not significantly increase overflow rate because every CPU can
  766. * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
  767. * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
  768. * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
  769. * overflow rate by 2x, which does not seem too significant.
  770. *
  771. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  772. * error if st_ino won't fit in target struct field. Use 32bit counter
  773. * here to attempt to avoid that.
  774. */
  775. #define LAST_INO_BATCH 1024
  776. static DEFINE_PER_CPU(unsigned int, last_ino);
  777. unsigned int get_next_ino(void)
  778. {
  779. unsigned int *p = &get_cpu_var(last_ino);
  780. unsigned int res = *p;
  781. #ifdef CONFIG_SMP
  782. if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
  783. static atomic_t shared_last_ino;
  784. int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
  785. res = next - LAST_INO_BATCH;
  786. }
  787. #endif
  788. *p = ++res;
  789. put_cpu_var(last_ino);
  790. return res;
  791. }
  792. EXPORT_SYMBOL(get_next_ino);
  793. /**
  794. * new_inode_pseudo - obtain an inode
  795. * @sb: superblock
  796. *
  797. * Allocates a new inode for given superblock.
  798. * Inode wont be chained in superblock s_inodes list
  799. * This means :
  800. * - fs can't be unmount
  801. * - quotas, fsnotify, writeback can't work
  802. */
  803. struct inode *new_inode_pseudo(struct super_block *sb)
  804. {
  805. struct inode *inode = alloc_inode(sb);
  806. if (inode) {
  807. spin_lock(&inode->i_lock);
  808. inode->i_state = 0;
  809. spin_unlock(&inode->i_lock);
  810. INIT_LIST_HEAD(&inode->i_sb_list);
  811. }
  812. return inode;
  813. }
  814. /**
  815. * new_inode - obtain an inode
  816. * @sb: superblock
  817. *
  818. * Allocates a new inode for given superblock. The default gfp_mask
  819. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  820. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  821. * for the page cache are not reclaimable or migratable,
  822. * mapping_set_gfp_mask() must be called with suitable flags on the
  823. * newly created inode's mapping
  824. *
  825. */
  826. struct inode *new_inode(struct super_block *sb)
  827. {
  828. struct inode *inode;
  829. spin_lock_prefetch(&inode_sb_list_lock);
  830. inode = new_inode_pseudo(sb);
  831. if (inode)
  832. inode_sb_list_add(inode);
  833. return inode;
  834. }
  835. EXPORT_SYMBOL(new_inode);
  836. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  837. void lockdep_annotate_inode_mutex_key(struct inode *inode)
  838. {
  839. if (S_ISDIR(inode->i_mode)) {
  840. struct file_system_type *type = inode->i_sb->s_type;
  841. /* Set new key only if filesystem hasn't already changed it */
  842. if (!lockdep_match_class(&inode->i_mutex,
  843. &type->i_mutex_key)) {
  844. /*
  845. * ensure nobody is actually holding i_mutex
  846. */
  847. mutex_destroy(&inode->i_mutex);
  848. mutex_init(&inode->i_mutex);
  849. lockdep_set_class(&inode->i_mutex,
  850. &type->i_mutex_dir_key);
  851. }
  852. }
  853. }
  854. EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
  855. #endif
  856. /**
  857. * unlock_new_inode - clear the I_NEW state and wake up any waiters
  858. * @inode: new inode to unlock
  859. *
  860. * Called when the inode is fully initialised to clear the new state of the
  861. * inode and wake up anyone waiting for the inode to finish initialisation.
  862. */
  863. void unlock_new_inode(struct inode *inode)
  864. {
  865. lockdep_annotate_inode_mutex_key(inode);
  866. spin_lock(&inode->i_lock);
  867. WARN_ON(!(inode->i_state & I_NEW));
  868. inode->i_state &= ~I_NEW;
  869. wake_up_bit(&inode->i_state, __I_NEW);
  870. spin_unlock(&inode->i_lock);
  871. }
  872. EXPORT_SYMBOL(unlock_new_inode);
  873. /**
  874. * iget5_locked - obtain an inode from a mounted file system
  875. * @sb: super block of file system
  876. * @hashval: hash value (usually inode number) to get
  877. * @test: callback used for comparisons between inodes
  878. * @set: callback used to initialize a new struct inode
  879. * @data: opaque data pointer to pass to @test and @set
  880. *
  881. * Search for the inode specified by @hashval and @data in the inode cache,
  882. * and if present it is return it with an increased reference count. This is
  883. * a generalized version of iget_locked() for file systems where the inode
  884. * number is not sufficient for unique identification of an inode.
  885. *
  886. * If the inode is not in cache, allocate a new inode and return it locked,
  887. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  888. * before unlocking it via unlock_new_inode().
  889. *
  890. * Note both @test and @set are called with the inode_hash_lock held, so can't
  891. * sleep.
  892. */
  893. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  894. int (*test)(struct inode *, void *),
  895. int (*set)(struct inode *, void *), void *data)
  896. {
  897. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  898. struct inode *inode;
  899. spin_lock(&inode_hash_lock);
  900. inode = find_inode(sb, head, test, data);
  901. spin_unlock(&inode_hash_lock);
  902. if (inode) {
  903. wait_on_inode(inode);
  904. return inode;
  905. }
  906. inode = alloc_inode(sb);
  907. if (inode) {
  908. struct inode *old;
  909. spin_lock(&inode_hash_lock);
  910. /* We released the lock, so.. */
  911. old = find_inode(sb, head, test, data);
  912. if (!old) {
  913. if (set(inode, data))
  914. goto set_failed;
  915. spin_lock(&inode->i_lock);
  916. inode->i_state = I_NEW;
  917. hlist_add_head(&inode->i_hash, head);
  918. spin_unlock(&inode->i_lock);
  919. inode_sb_list_add(inode);
  920. spin_unlock(&inode_hash_lock);
  921. /* Return the locked inode with I_NEW set, the
  922. * caller is responsible for filling in the contents
  923. */
  924. return inode;
  925. }
  926. /*
  927. * Uhhuh, somebody else created the same inode under
  928. * us. Use the old inode instead of the one we just
  929. * allocated.
  930. */
  931. spin_unlock(&inode_hash_lock);
  932. destroy_inode(inode);
  933. inode = old;
  934. wait_on_inode(inode);
  935. }
  936. return inode;
  937. set_failed:
  938. spin_unlock(&inode_hash_lock);
  939. destroy_inode(inode);
  940. return NULL;
  941. }
  942. EXPORT_SYMBOL(iget5_locked);
  943. /**
  944. * iget_locked - obtain an inode from a mounted file system
  945. * @sb: super block of file system
  946. * @ino: inode number to get
  947. *
  948. * Search for the inode specified by @ino in the inode cache and if present
  949. * return it with an increased reference count. This is for file systems
  950. * where the inode number is sufficient for unique identification of an inode.
  951. *
  952. * If the inode is not in cache, allocate a new inode and return it locked,
  953. * hashed, and with the I_NEW flag set. The file system gets to fill it in
  954. * before unlocking it via unlock_new_inode().
  955. */
  956. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  957. {
  958. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  959. struct inode *inode;
  960. spin_lock(&inode_hash_lock);
  961. inode = find_inode_fast(sb, head, ino);
  962. spin_unlock(&inode_hash_lock);
  963. if (inode) {
  964. wait_on_inode(inode);
  965. return inode;
  966. }
  967. inode = alloc_inode(sb);
  968. if (inode) {
  969. struct inode *old;
  970. spin_lock(&inode_hash_lock);
  971. /* We released the lock, so.. */
  972. old = find_inode_fast(sb, head, ino);
  973. if (!old) {
  974. inode->i_ino = ino;
  975. spin_lock(&inode->i_lock);
  976. inode->i_state = I_NEW;
  977. hlist_add_head(&inode->i_hash, head);
  978. spin_unlock(&inode->i_lock);
  979. inode_sb_list_add(inode);
  980. spin_unlock(&inode_hash_lock);
  981. /* Return the locked inode with I_NEW set, the
  982. * caller is responsible for filling in the contents
  983. */
  984. return inode;
  985. }
  986. /*
  987. * Uhhuh, somebody else created the same inode under
  988. * us. Use the old inode instead of the one we just
  989. * allocated.
  990. */
  991. spin_unlock(&inode_hash_lock);
  992. destroy_inode(inode);
  993. inode = old;
  994. wait_on_inode(inode);
  995. }
  996. return inode;
  997. }
  998. EXPORT_SYMBOL(iget_locked);
  999. /*
  1000. * search the inode cache for a matching inode number.
  1001. * If we find one, then the inode number we are trying to
  1002. * allocate is not unique and so we should not use it.
  1003. *
  1004. * Returns 1 if the inode number is unique, 0 if it is not.
  1005. */
  1006. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  1007. {
  1008. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  1009. struct hlist_node *node;
  1010. struct inode *inode;
  1011. spin_lock(&inode_hash_lock);
  1012. hlist_for_each_entry(inode, node, b, i_hash) {
  1013. if (inode->i_ino == ino && inode->i_sb == sb) {
  1014. spin_unlock(&inode_hash_lock);
  1015. return 0;
  1016. }
  1017. }
  1018. spin_unlock(&inode_hash_lock);
  1019. return 1;
  1020. }
  1021. /**
  1022. * iunique - get a unique inode number
  1023. * @sb: superblock
  1024. * @max_reserved: highest reserved inode number
  1025. *
  1026. * Obtain an inode number that is unique on the system for a given
  1027. * superblock. This is used by file systems that have no natural
  1028. * permanent inode numbering system. An inode number is returned that
  1029. * is higher than the reserved limit but unique.
  1030. *
  1031. * BUGS:
  1032. * With a large number of inodes live on the file system this function
  1033. * currently becomes quite slow.
  1034. */
  1035. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  1036. {
  1037. /*
  1038. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  1039. * error if st_ino won't fit in target struct field. Use 32bit counter
  1040. * here to attempt to avoid that.
  1041. */
  1042. static DEFINE_SPINLOCK(iunique_lock);
  1043. static unsigned int counter;
  1044. ino_t res;
  1045. spin_lock(&iunique_lock);
  1046. do {
  1047. if (counter <= max_reserved)
  1048. counter = max_reserved + 1;
  1049. res = counter++;
  1050. } while (!test_inode_iunique(sb, res));
  1051. spin_unlock(&iunique_lock);
  1052. return res;
  1053. }
  1054. EXPORT_SYMBOL(iunique);
  1055. struct inode *igrab(struct inode *inode)
  1056. {
  1057. spin_lock(&inode->i_lock);
  1058. if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
  1059. __iget(inode);
  1060. spin_unlock(&inode->i_lock);
  1061. } else {
  1062. spin_unlock(&inode->i_lock);
  1063. /*
  1064. * Handle the case where s_op->clear_inode is not been
  1065. * called yet, and somebody is calling igrab
  1066. * while the inode is getting freed.
  1067. */
  1068. inode = NULL;
  1069. }
  1070. return inode;
  1071. }
  1072. EXPORT_SYMBOL(igrab);
  1073. /**
  1074. * ilookup5_nowait - search for an inode in the inode cache
  1075. * @sb: super block of file system to search
  1076. * @hashval: hash value (usually inode number) to search for
  1077. * @test: callback used for comparisons between inodes
  1078. * @data: opaque data pointer to pass to @test
  1079. *
  1080. * Search for the inode specified by @hashval and @data in the inode cache.
  1081. * If the inode is in the cache, the inode is returned with an incremented
  1082. * reference count.
  1083. *
  1084. * Note: I_NEW is not waited upon so you have to be very careful what you do
  1085. * with the returned inode. You probably should be using ilookup5() instead.
  1086. *
  1087. * Note2: @test is called with the inode_hash_lock held, so can't sleep.
  1088. */
  1089. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1090. int (*test)(struct inode *, void *), void *data)
  1091. {
  1092. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1093. struct inode *inode;
  1094. spin_lock(&inode_hash_lock);
  1095. inode = find_inode(sb, head, test, data);
  1096. spin_unlock(&inode_hash_lock);
  1097. return inode;
  1098. }
  1099. EXPORT_SYMBOL(ilookup5_nowait);
  1100. /**
  1101. * ilookup5 - search for an inode in the inode cache
  1102. * @sb: super block of file system to search
  1103. * @hashval: hash value (usually inode number) to search for
  1104. * @test: callback used for comparisons between inodes
  1105. * @data: opaque data pointer to pass to @test
  1106. *
  1107. * Search for the inode specified by @hashval and @data in the inode cache,
  1108. * and if the inode is in the cache, return the inode with an incremented
  1109. * reference count. Waits on I_NEW before returning the inode.
  1110. * returned with an incremented reference count.
  1111. *
  1112. * This is a generalized version of ilookup() for file systems where the
  1113. * inode number is not sufficient for unique identification of an inode.
  1114. *
  1115. * Note: @test is called with the inode_hash_lock held, so can't sleep.
  1116. */
  1117. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1118. int (*test)(struct inode *, void *), void *data)
  1119. {
  1120. struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
  1121. if (inode)
  1122. wait_on_inode(inode);
  1123. return inode;
  1124. }
  1125. EXPORT_SYMBOL(ilookup5);
  1126. /**
  1127. * ilookup - search for an inode in the inode cache
  1128. * @sb: super block of file system to search
  1129. * @ino: inode number to search for
  1130. *
  1131. * Search for the inode @ino in the inode cache, and if the inode is in the
  1132. * cache, the inode is returned with an incremented reference count.
  1133. */
  1134. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1135. {
  1136. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1137. struct inode *inode;
  1138. spin_lock(&inode_hash_lock);
  1139. inode = find_inode_fast(sb, head, ino);
  1140. spin_unlock(&inode_hash_lock);
  1141. if (inode)
  1142. wait_on_inode(inode);
  1143. return inode;
  1144. }
  1145. EXPORT_SYMBOL(ilookup);
  1146. int insert_inode_locked(struct inode *inode)
  1147. {
  1148. struct super_block *sb = inode->i_sb;
  1149. ino_t ino = inode->i_ino;
  1150. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1151. while (1) {
  1152. struct hlist_node *node;
  1153. struct inode *old = NULL;
  1154. spin_lock(&inode_hash_lock);
  1155. hlist_for_each_entry(old, node, head, i_hash) {
  1156. if (old->i_ino != ino)
  1157. continue;
  1158. if (old->i_sb != sb)
  1159. continue;
  1160. spin_lock(&old->i_lock);
  1161. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1162. spin_unlock(&old->i_lock);
  1163. continue;
  1164. }
  1165. break;
  1166. }
  1167. if (likely(!node)) {
  1168. spin_lock(&inode->i_lock);
  1169. inode->i_state |= I_NEW;
  1170. hlist_add_head(&inode->i_hash, head);
  1171. spin_unlock(&inode->i_lock);
  1172. spin_unlock(&inode_hash_lock);
  1173. return 0;
  1174. }
  1175. __iget(old);
  1176. spin_unlock(&old->i_lock);
  1177. spin_unlock(&inode_hash_lock);
  1178. wait_on_inode(old);
  1179. if (unlikely(!inode_unhashed(old))) {
  1180. iput(old);
  1181. return -EBUSY;
  1182. }
  1183. iput(old);
  1184. }
  1185. }
  1186. EXPORT_SYMBOL(insert_inode_locked);
  1187. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1188. int (*test)(struct inode *, void *), void *data)
  1189. {
  1190. struct super_block *sb = inode->i_sb;
  1191. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1192. while (1) {
  1193. struct hlist_node *node;
  1194. struct inode *old = NULL;
  1195. spin_lock(&inode_hash_lock);
  1196. hlist_for_each_entry(old, node, head, i_hash) {
  1197. if (old->i_sb != sb)
  1198. continue;
  1199. if (!test(old, data))
  1200. continue;
  1201. spin_lock(&old->i_lock);
  1202. if (old->i_state & (I_FREEING|I_WILL_FREE)) {
  1203. spin_unlock(&old->i_lock);
  1204. continue;
  1205. }
  1206. break;
  1207. }
  1208. if (likely(!node)) {
  1209. spin_lock(&inode->i_lock);
  1210. inode->i_state |= I_NEW;
  1211. hlist_add_head(&inode->i_hash, head);
  1212. spin_unlock(&inode->i_lock);
  1213. spin_unlock(&inode_hash_lock);
  1214. return 0;
  1215. }
  1216. __iget(old);
  1217. spin_unlock(&old->i_lock);
  1218. spin_unlock(&inode_hash_lock);
  1219. wait_on_inode(old);
  1220. if (unlikely(!inode_unhashed(old))) {
  1221. iput(old);
  1222. return -EBUSY;
  1223. }
  1224. iput(old);
  1225. }
  1226. }
  1227. EXPORT_SYMBOL(insert_inode_locked4);
  1228. int generic_delete_inode(struct inode *inode)
  1229. {
  1230. return 1;
  1231. }
  1232. EXPORT_SYMBOL(generic_delete_inode);
  1233. /*
  1234. * Normal UNIX filesystem behaviour: delete the
  1235. * inode when the usage count drops to zero, and
  1236. * i_nlink is zero.
  1237. */
  1238. int generic_drop_inode(struct inode *inode)
  1239. {
  1240. return !inode->i_nlink || inode_unhashed(inode);
  1241. }
  1242. EXPORT_SYMBOL_GPL(generic_drop_inode);
  1243. /*
  1244. * Called when we're dropping the last reference
  1245. * to an inode.
  1246. *
  1247. * Call the FS "drop_inode()" function, defaulting to
  1248. * the legacy UNIX filesystem behaviour. If it tells
  1249. * us to evict inode, do so. Otherwise, retain inode
  1250. * in cache if fs is alive, sync and evict if fs is
  1251. * shutting down.
  1252. */
  1253. static void iput_final(struct inode *inode)
  1254. {
  1255. struct super_block *sb = inode->i_sb;
  1256. const struct super_operations *op = inode->i_sb->s_op;
  1257. int drop;
  1258. WARN_ON(inode->i_state & I_NEW);
  1259. if (op->drop_inode)
  1260. drop = op->drop_inode(inode);
  1261. else
  1262. drop = generic_drop_inode(inode);
  1263. if (!drop && (sb->s_flags & MS_ACTIVE)) {
  1264. inode->i_state |= I_REFERENCED;
  1265. if (!(inode->i_state & (I_DIRTY|I_SYNC)))
  1266. inode_lru_list_add(inode);
  1267. spin_unlock(&inode->i_lock);
  1268. return;
  1269. }
  1270. if (!drop) {
  1271. inode->i_state |= I_WILL_FREE;
  1272. spin_unlock(&inode->i_lock);
  1273. write_inode_now(inode, 1);
  1274. spin_lock(&inode->i_lock);
  1275. WARN_ON(inode->i_state & I_NEW);
  1276. inode->i_state &= ~I_WILL_FREE;
  1277. }
  1278. inode->i_state |= I_FREEING;
  1279. if (!list_empty(&inode->i_lru))
  1280. inode_lru_list_del(inode);
  1281. spin_unlock(&inode->i_lock);
  1282. evict(inode);
  1283. }
  1284. /**
  1285. * iput - put an inode
  1286. * @inode: inode to put
  1287. *
  1288. * Puts an inode, dropping its usage count. If the inode use count hits
  1289. * zero, the inode is then freed and may also be destroyed.
  1290. *
  1291. * Consequently, iput() can sleep.
  1292. */
  1293. void iput(struct inode *inode)
  1294. {
  1295. if (inode) {
  1296. BUG_ON(inode->i_state & I_CLEAR);
  1297. if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
  1298. iput_final(inode);
  1299. }
  1300. }
  1301. EXPORT_SYMBOL(iput);
  1302. /**
  1303. * bmap - find a block number in a file
  1304. * @inode: inode of file
  1305. * @block: block to find
  1306. *
  1307. * Returns the block number on the device holding the inode that
  1308. * is the disk block number for the block of the file requested.
  1309. * That is, asked for block 4 of inode 1 the function will return the
  1310. * disk block relative to the disk start that holds that block of the
  1311. * file.
  1312. */
  1313. sector_t bmap(struct inode *inode, sector_t block)
  1314. {
  1315. sector_t res = 0;
  1316. if (inode->i_mapping->a_ops->bmap)
  1317. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1318. return res;
  1319. }
  1320. EXPORT_SYMBOL(bmap);
  1321. /*
  1322. * With relative atime, only update atime if the previous atime is
  1323. * earlier than either the ctime or mtime or if at least a day has
  1324. * passed since the last atime update.
  1325. */
  1326. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1327. struct timespec now)
  1328. {
  1329. if (!(mnt->mnt_flags & MNT_RELATIME))
  1330. return 1;
  1331. /*
  1332. * Is mtime younger than atime? If yes, update atime:
  1333. */
  1334. if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1335. return 1;
  1336. /*
  1337. * Is ctime younger than atime? If yes, update atime:
  1338. */
  1339. if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1340. return 1;
  1341. /*
  1342. * Is the previous atime value older than a day? If yes,
  1343. * update atime:
  1344. */
  1345. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1346. return 1;
  1347. /*
  1348. * Good, we can skip the atime update:
  1349. */
  1350. return 0;
  1351. }
  1352. /**
  1353. * touch_atime - update the access time
  1354. * @mnt: mount the inode is accessed on
  1355. * @dentry: dentry accessed
  1356. *
  1357. * Update the accessed time on an inode and mark it for writeback.
  1358. * This function automatically handles read only file systems and media,
  1359. * as well as the "noatime" flag and inode specific "noatime" markers.
  1360. */
  1361. void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
  1362. {
  1363. struct inode *inode = dentry->d_inode;
  1364. struct timespec now;
  1365. if (inode->i_flags & S_NOATIME)
  1366. return;
  1367. if (IS_NOATIME(inode))
  1368. return;
  1369. if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  1370. return;
  1371. if (mnt->mnt_flags & MNT_NOATIME)
  1372. return;
  1373. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1374. return;
  1375. now = current_fs_time(inode->i_sb);
  1376. if (!relatime_need_update(mnt, inode, now))
  1377. return;
  1378. if (timespec_equal(&inode->i_atime, &now))
  1379. return;
  1380. if (mnt_want_write(mnt))
  1381. return;
  1382. inode->i_atime = now;
  1383. mark_inode_dirty_sync(inode);
  1384. mnt_drop_write(mnt);
  1385. }
  1386. EXPORT_SYMBOL(touch_atime);
  1387. /**
  1388. * file_update_time - update mtime and ctime time
  1389. * @file: file accessed
  1390. *
  1391. * Update the mtime and ctime members of an inode and mark the inode
  1392. * for writeback. Note that this function is meant exclusively for
  1393. * usage in the file write path of filesystems, and filesystems may
  1394. * choose to explicitly ignore update via this function with the
  1395. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1396. * timestamps are handled by the server.
  1397. */
  1398. void file_update_time(struct file *file)
  1399. {
  1400. struct inode *inode = file->f_path.dentry->d_inode;
  1401. struct timespec now;
  1402. enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
  1403. /* First try to exhaust all avenues to not sync */
  1404. if (IS_NOCMTIME(inode))
  1405. return;
  1406. now = current_fs_time(inode->i_sb);
  1407. if (!timespec_equal(&inode->i_mtime, &now))
  1408. sync_it = S_MTIME;
  1409. if (!timespec_equal(&inode->i_ctime, &now))
  1410. sync_it |= S_CTIME;
  1411. if (IS_I_VERSION(inode))
  1412. sync_it |= S_VERSION;
  1413. if (!sync_it)
  1414. return;
  1415. /* Finally allowed to write? Takes lock. */
  1416. if (mnt_want_write_file(file))
  1417. return;
  1418. /* Only change inode inside the lock region */
  1419. if (sync_it & S_VERSION)
  1420. inode_inc_iversion(inode);
  1421. if (sync_it & S_CTIME)
  1422. inode->i_ctime = now;
  1423. if (sync_it & S_MTIME)
  1424. inode->i_mtime = now;
  1425. mark_inode_dirty_sync(inode);
  1426. mnt_drop_write_file(file);
  1427. }
  1428. EXPORT_SYMBOL(file_update_time);
  1429. int inode_needs_sync(struct inode *inode)
  1430. {
  1431. if (IS_SYNC(inode))
  1432. return 1;
  1433. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1434. return 1;
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL(inode_needs_sync);
  1438. int inode_wait(void *word)
  1439. {
  1440. schedule();
  1441. return 0;
  1442. }
  1443. EXPORT_SYMBOL(inode_wait);
  1444. /*
  1445. * If we try to find an inode in the inode hash while it is being
  1446. * deleted, we have to wait until the filesystem completes its
  1447. * deletion before reporting that it isn't found. This function waits
  1448. * until the deletion _might_ have completed. Callers are responsible
  1449. * to recheck inode state.
  1450. *
  1451. * It doesn't matter if I_NEW is not set initially, a call to
  1452. * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
  1453. * will DTRT.
  1454. */
  1455. static void __wait_on_freeing_inode(struct inode *inode)
  1456. {
  1457. wait_queue_head_t *wq;
  1458. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
  1459. wq = bit_waitqueue(&inode->i_state, __I_NEW);
  1460. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1461. spin_unlock(&inode->i_lock);
  1462. spin_unlock(&inode_hash_lock);
  1463. schedule();
  1464. finish_wait(wq, &wait.wait);
  1465. spin_lock(&inode_hash_lock);
  1466. }
  1467. static __initdata unsigned long ihash_entries;
  1468. static int __init set_ihash_entries(char *str)
  1469. {
  1470. if (!str)
  1471. return 0;
  1472. ihash_entries = simple_strtoul(str, &str, 0);
  1473. return 1;
  1474. }
  1475. __setup("ihash_entries=", set_ihash_entries);
  1476. /*
  1477. * Initialize the waitqueues and inode hash table.
  1478. */
  1479. void __init inode_init_early(void)
  1480. {
  1481. int loop;
  1482. /* If hashes are distributed across NUMA nodes, defer
  1483. * hash allocation until vmalloc space is available.
  1484. */
  1485. if (hashdist)
  1486. return;
  1487. inode_hashtable =
  1488. alloc_large_system_hash("Inode-cache",
  1489. sizeof(struct hlist_head),
  1490. ihash_entries,
  1491. 14,
  1492. HASH_EARLY,
  1493. &i_hash_shift,
  1494. &i_hash_mask,
  1495. 0);
  1496. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1497. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1498. }
  1499. void __init inode_init(void)
  1500. {
  1501. int loop;
  1502. /* inode slab cache */
  1503. inode_cachep = kmem_cache_create("inode_cache",
  1504. sizeof(struct inode),
  1505. 0,
  1506. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1507. SLAB_MEM_SPREAD),
  1508. init_once);
  1509. /* Hash may have been set up in inode_init_early */
  1510. if (!hashdist)
  1511. return;
  1512. inode_hashtable =
  1513. alloc_large_system_hash("Inode-cache",
  1514. sizeof(struct hlist_head),
  1515. ihash_entries,
  1516. 14,
  1517. 0,
  1518. &i_hash_shift,
  1519. &i_hash_mask,
  1520. 0);
  1521. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1522. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1523. }
  1524. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1525. {
  1526. inode->i_mode = mode;
  1527. if (S_ISCHR(mode)) {
  1528. inode->i_fop = &def_chr_fops;
  1529. inode->i_rdev = rdev;
  1530. } else if (S_ISBLK(mode)) {
  1531. inode->i_fop = &def_blk_fops;
  1532. inode->i_rdev = rdev;
  1533. } else if (S_ISFIFO(mode))
  1534. inode->i_fop = &def_fifo_fops;
  1535. else if (S_ISSOCK(mode))
  1536. inode->i_fop = &bad_sock_fops;
  1537. else
  1538. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1539. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1540. inode->i_ino);
  1541. }
  1542. EXPORT_SYMBOL(init_special_inode);
  1543. /**
  1544. * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
  1545. * @inode: New inode
  1546. * @dir: Directory inode
  1547. * @mode: mode of the new inode
  1548. */
  1549. void inode_init_owner(struct inode *inode, const struct inode *dir,
  1550. umode_t mode)
  1551. {
  1552. inode->i_uid = current_fsuid();
  1553. if (dir && dir->i_mode & S_ISGID) {
  1554. inode->i_gid = dir->i_gid;
  1555. if (S_ISDIR(mode))
  1556. mode |= S_ISGID;
  1557. } else
  1558. inode->i_gid = current_fsgid();
  1559. inode->i_mode = mode;
  1560. }
  1561. EXPORT_SYMBOL(inode_init_owner);
  1562. /**
  1563. * inode_owner_or_capable - check current task permissions to inode
  1564. * @inode: inode being checked
  1565. *
  1566. * Return true if current either has CAP_FOWNER to the inode, or
  1567. * owns the file.
  1568. */
  1569. bool inode_owner_or_capable(const struct inode *inode)
  1570. {
  1571. struct user_namespace *ns = inode_userns(inode);
  1572. if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
  1573. return true;
  1574. if (ns_capable(ns, CAP_FOWNER))
  1575. return true;
  1576. return false;
  1577. }
  1578. EXPORT_SYMBOL(inode_owner_or_capable);