inode.c 48 KB

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