| Nicholas Flintham | 9fafe90 | 2014-03-31 13:51:31 +0100 | [diff] [blame] | 1 | #include <linux/export.h> |
| 2 | #include <linux/fs.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <linux/backing-dev.h> |
| 5 | #include <linux/hash.h> |
| 6 | #include <linux/swap.h> |
| 7 | #include <linux/security.h> |
| 8 | #include <linux/cdev.h> |
| 9 | #include <linux/bootmem.h> |
| 10 | #include <linux/fsnotify.h> |
| 11 | #include <linux/mount.h> |
| 12 | #include <linux/posix_acl.h> |
| 13 | #include <linux/prefetch.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/ratelimit.h> |
| 16 | #include "internal.h" |
| 17 | |
| 18 | |
| 19 | static unsigned int i_hash_mask __read_mostly; |
| 20 | static unsigned int i_hash_shift __read_mostly; |
| 21 | static struct hlist_head *inode_hashtable __read_mostly; |
| 22 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); |
| 23 | |
| 24 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock); |
| 25 | |
| 26 | const struct address_space_operations empty_aops = { |
| 27 | }; |
| 28 | EXPORT_SYMBOL(empty_aops); |
| 29 | |
| 30 | struct inodes_stat_t inodes_stat; |
| 31 | |
| 32 | static DEFINE_PER_CPU(unsigned int, nr_inodes); |
| 33 | static DEFINE_PER_CPU(unsigned int, nr_unused); |
| 34 | |
| 35 | static struct kmem_cache *inode_cachep __read_mostly; |
| 36 | |
| 37 | static int get_nr_inodes(void) |
| 38 | { |
| 39 | int i; |
| 40 | int sum = 0; |
| 41 | for_each_possible_cpu(i) |
| 42 | sum += per_cpu(nr_inodes, i); |
| 43 | return sum < 0 ? 0 : sum; |
| 44 | } |
| 45 | |
| 46 | static inline int get_nr_inodes_unused(void) |
| 47 | { |
| 48 | int i; |
| 49 | int sum = 0; |
| 50 | for_each_possible_cpu(i) |
| 51 | sum += per_cpu(nr_unused, i); |
| 52 | return sum < 0 ? 0 : sum; |
| 53 | } |
| 54 | |
| 55 | int get_nr_dirty_inodes(void) |
| 56 | { |
| 57 | |
| 58 | int nr_dirty = get_nr_inodes() - get_nr_inodes_unused(); |
| 59 | return nr_dirty > 0 ? nr_dirty : 0; |
| 60 | } |
| 61 | |
| 62 | #ifdef CONFIG_SYSCTL |
| 63 | int proc_nr_inodes(ctl_table *table, int write, |
| 64 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 65 | { |
| 66 | inodes_stat.nr_inodes = get_nr_inodes(); |
| 67 | inodes_stat.nr_unused = get_nr_inodes_unused(); |
| 68 | return proc_dointvec(table, write, buffer, lenp, ppos); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | int inode_init_always(struct super_block *sb, struct inode *inode) |
| 73 | { |
| 74 | static const struct inode_operations empty_iops; |
| 75 | static const struct file_operations empty_fops; |
| 76 | struct address_space *const mapping = &inode->i_data; |
| 77 | |
| 78 | inode->i_sb = sb; |
| 79 | inode->i_blkbits = sb->s_blocksize_bits; |
| 80 | inode->i_flags = 0; |
| 81 | atomic_set(&inode->i_count, 1); |
| 82 | inode->i_op = &empty_iops; |
| 83 | inode->i_fop = &empty_fops; |
| 84 | inode->__i_nlink = 1; |
| 85 | inode->i_opflags = 0; |
| 86 | inode->i_uid = 0; |
| 87 | inode->i_gid = 0; |
| 88 | atomic_set(&inode->i_writecount, 0); |
| 89 | inode->i_size = 0; |
| 90 | inode->i_blocks = 0; |
| 91 | inode->i_bytes = 0; |
| 92 | inode->i_generation = 0; |
| 93 | #ifdef CONFIG_QUOTA |
| 94 | memset(&inode->i_dquot, 0, sizeof(inode->i_dquot)); |
| 95 | #endif |
| 96 | inode->i_pipe = NULL; |
| 97 | inode->i_bdev = NULL; |
| 98 | inode->i_cdev = NULL; |
| 99 | inode->i_rdev = 0; |
| 100 | inode->dirtied_when = 0; |
| 101 | |
| 102 | if (security_inode_alloc(inode)) |
| 103 | goto out; |
| 104 | spin_lock_init(&inode->i_lock); |
| 105 | lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); |
| 106 | |
| 107 | mutex_init(&inode->i_mutex); |
| 108 | lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key); |
| 109 | |
| 110 | atomic_set(&inode->i_dio_count, 0); |
| 111 | |
| 112 | mapping->a_ops = &empty_aops; |
| 113 | mapping->host = inode; |
| 114 | mapping->flags = 0; |
| 115 | mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE); |
| 116 | mapping->assoc_mapping = NULL; |
| 117 | mapping->backing_dev_info = &default_backing_dev_info; |
| 118 | mapping->writeback_index = 0; |
| 119 | |
| 120 | if (sb->s_bdev) { |
| 121 | struct backing_dev_info *bdi; |
| 122 | |
| 123 | bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info; |
| 124 | mapping->backing_dev_info = bdi; |
| 125 | } |
| 126 | inode->i_private = NULL; |
| 127 | inode->i_mapping = mapping; |
| 128 | INIT_LIST_HEAD(&inode->i_dentry); |
| 129 | #ifdef CONFIG_FS_POSIX_ACL |
| 130 | inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; |
| 131 | #endif |
| 132 | |
| 133 | #ifdef CONFIG_FSNOTIFY |
| 134 | inode->i_fsnotify_mask = 0; |
| 135 | #endif |
| 136 | |
| 137 | this_cpu_inc(nr_inodes); |
| 138 | |
| 139 | return 0; |
| 140 | out: |
| 141 | return -ENOMEM; |
| 142 | } |
| 143 | EXPORT_SYMBOL(inode_init_always); |
| 144 | |
| 145 | static struct inode *alloc_inode(struct super_block *sb) |
| 146 | { |
| 147 | struct inode *inode; |
| 148 | |
| 149 | if (sb->s_op->alloc_inode) |
| 150 | inode = sb->s_op->alloc_inode(sb); |
| 151 | else |
| 152 | inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL); |
| 153 | |
| 154 | if (!inode) |
| 155 | return NULL; |
| 156 | |
| 157 | if (unlikely(inode_init_always(sb, inode))) { |
| 158 | if (inode->i_sb->s_op->destroy_inode) |
| 159 | inode->i_sb->s_op->destroy_inode(inode); |
| 160 | else |
| 161 | kmem_cache_free(inode_cachep, inode); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | return inode; |
| 166 | } |
| 167 | |
| 168 | void free_inode_nonrcu(struct inode *inode) |
| 169 | { |
| 170 | kmem_cache_free(inode_cachep, inode); |
| 171 | } |
| 172 | EXPORT_SYMBOL(free_inode_nonrcu); |
| 173 | |
| 174 | void __destroy_inode(struct inode *inode) |
| 175 | { |
| 176 | BUG_ON(inode_has_buffers(inode)); |
| 177 | security_inode_free(inode); |
| 178 | fsnotify_inode_delete(inode); |
| 179 | if (!inode->i_nlink) { |
| 180 | WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0); |
| 181 | atomic_long_dec(&inode->i_sb->s_remove_count); |
| 182 | } |
| 183 | |
| 184 | #ifdef CONFIG_FS_POSIX_ACL |
| 185 | if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED) |
| 186 | posix_acl_release(inode->i_acl); |
| 187 | if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED) |
| 188 | posix_acl_release(inode->i_default_acl); |
| 189 | #endif |
| 190 | this_cpu_dec(nr_inodes); |
| 191 | } |
| 192 | EXPORT_SYMBOL(__destroy_inode); |
| 193 | |
| 194 | static void i_callback(struct rcu_head *head) |
| 195 | { |
| 196 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 197 | kmem_cache_free(inode_cachep, inode); |
| 198 | } |
| 199 | |
| 200 | static void destroy_inode(struct inode *inode) |
| 201 | { |
| 202 | BUG_ON(!list_empty(&inode->i_lru)); |
| 203 | __destroy_inode(inode); |
| 204 | if (inode->i_sb->s_op->destroy_inode) |
| 205 | inode->i_sb->s_op->destroy_inode(inode); |
| 206 | else |
| 207 | call_rcu(&inode->i_rcu, i_callback); |
| 208 | } |
| 209 | |
| 210 | void drop_nlink(struct inode *inode) |
| 211 | { |
| 212 | WARN_ON(inode->i_nlink == 0); |
| 213 | inode->__i_nlink--; |
| 214 | if (!inode->i_nlink) |
| 215 | atomic_long_inc(&inode->i_sb->s_remove_count); |
| 216 | } |
| 217 | EXPORT_SYMBOL(drop_nlink); |
| 218 | |
| 219 | void clear_nlink(struct inode *inode) |
| 220 | { |
| 221 | if (inode->i_nlink) { |
| 222 | inode->__i_nlink = 0; |
| 223 | atomic_long_inc(&inode->i_sb->s_remove_count); |
| 224 | } |
| 225 | } |
| 226 | EXPORT_SYMBOL(clear_nlink); |
| 227 | |
| 228 | void set_nlink(struct inode *inode, unsigned int nlink) |
| 229 | { |
| 230 | if (!nlink) { |
| 231 | clear_nlink(inode); |
| 232 | } else { |
| 233 | |
| 234 | if (inode->i_nlink == 0) |
| 235 | atomic_long_dec(&inode->i_sb->s_remove_count); |
| 236 | |
| 237 | inode->__i_nlink = nlink; |
| 238 | } |
| 239 | } |
| 240 | EXPORT_SYMBOL(set_nlink); |
| 241 | |
| 242 | void inc_nlink(struct inode *inode) |
| 243 | { |
| 244 | if (WARN_ON(inode->i_nlink == 0)) |
| 245 | atomic_long_dec(&inode->i_sb->s_remove_count); |
| 246 | |
| 247 | inode->__i_nlink++; |
| 248 | } |
| 249 | EXPORT_SYMBOL(inc_nlink); |
| 250 | |
| 251 | void address_space_init_once(struct address_space *mapping) |
| 252 | { |
| 253 | memset(mapping, 0, sizeof(*mapping)); |
| 254 | INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC); |
| 255 | spin_lock_init(&mapping->tree_lock); |
| 256 | mutex_init(&mapping->i_mmap_mutex); |
| 257 | INIT_LIST_HEAD(&mapping->private_list); |
| 258 | spin_lock_init(&mapping->private_lock); |
| 259 | INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap); |
| 260 | INIT_LIST_HEAD(&mapping->i_mmap_nonlinear); |
| 261 | } |
| 262 | EXPORT_SYMBOL(address_space_init_once); |
| 263 | |
| 264 | void inode_init_once(struct inode *inode) |
| 265 | { |
| 266 | memset(inode, 0, sizeof(*inode)); |
| 267 | INIT_HLIST_NODE(&inode->i_hash); |
| 268 | INIT_LIST_HEAD(&inode->i_devices); |
| 269 | INIT_LIST_HEAD(&inode->i_wb_list); |
| 270 | INIT_LIST_HEAD(&inode->i_lru); |
| 271 | address_space_init_once(&inode->i_data); |
| 272 | i_size_ordered_init(inode); |
| 273 | #ifdef CONFIG_FSNOTIFY |
| 274 | INIT_HLIST_HEAD(&inode->i_fsnotify_marks); |
| 275 | #endif |
| 276 | } |
| 277 | EXPORT_SYMBOL(inode_init_once); |
| 278 | |
| 279 | static void init_once(void *foo) |
| 280 | { |
| 281 | struct inode *inode = (struct inode *) foo; |
| 282 | |
| 283 | inode_init_once(inode); |
| 284 | } |
| 285 | |
| 286 | void __iget(struct inode *inode) |
| 287 | { |
| 288 | atomic_inc(&inode->i_count); |
| 289 | } |
| 290 | |
| 291 | void ihold(struct inode *inode) |
| 292 | { |
| 293 | WARN_ON(atomic_inc_return(&inode->i_count) < 2); |
| 294 | } |
| 295 | EXPORT_SYMBOL(ihold); |
| 296 | |
| 297 | static void inode_lru_list_add(struct inode *inode) |
| 298 | { |
| 299 | spin_lock(&inode->i_sb->s_inode_lru_lock); |
| 300 | if (list_empty(&inode->i_lru)) { |
| 301 | list_add(&inode->i_lru, &inode->i_sb->s_inode_lru); |
| 302 | inode->i_sb->s_nr_inodes_unused++; |
| 303 | this_cpu_inc(nr_unused); |
| 304 | } |
| 305 | spin_unlock(&inode->i_sb->s_inode_lru_lock); |
| 306 | } |
| 307 | |
| 308 | static void inode_lru_list_del(struct inode *inode) |
| 309 | { |
| 310 | spin_lock(&inode->i_sb->s_inode_lru_lock); |
| 311 | if (!list_empty(&inode->i_lru)) { |
| 312 | list_del_init(&inode->i_lru); |
| 313 | inode->i_sb->s_nr_inodes_unused--; |
| 314 | this_cpu_dec(nr_unused); |
| 315 | } |
| 316 | spin_unlock(&inode->i_sb->s_inode_lru_lock); |
| 317 | } |
| 318 | |
| 319 | void inode_sb_list_add(struct inode *inode) |
| 320 | { |
| 321 | spin_lock(&inode_sb_list_lock); |
| 322 | list_add(&inode->i_sb_list, &inode->i_sb->s_inodes); |
| 323 | spin_unlock(&inode_sb_list_lock); |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(inode_sb_list_add); |
| 326 | |
| 327 | static inline void inode_sb_list_del(struct inode *inode) |
| 328 | { |
| 329 | if (!list_empty(&inode->i_sb_list)) { |
| 330 | spin_lock(&inode_sb_list_lock); |
| 331 | list_del_init(&inode->i_sb_list); |
| 332 | spin_unlock(&inode_sb_list_lock); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static unsigned long hash(struct super_block *sb, unsigned long hashval) |
| 337 | { |
| 338 | unsigned long tmp; |
| 339 | |
| 340 | tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / |
| 341 | L1_CACHE_BYTES; |
| 342 | tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift); |
| 343 | return tmp & i_hash_mask; |
| 344 | } |
| 345 | |
| 346 | void __insert_inode_hash(struct inode *inode, unsigned long hashval) |
| 347 | { |
| 348 | struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); |
| 349 | |
| 350 | spin_lock(&inode_hash_lock); |
| 351 | spin_lock(&inode->i_lock); |
| 352 | hlist_add_head(&inode->i_hash, b); |
| 353 | spin_unlock(&inode->i_lock); |
| 354 | spin_unlock(&inode_hash_lock); |
| 355 | } |
| 356 | EXPORT_SYMBOL(__insert_inode_hash); |
| 357 | |
| 358 | void __remove_inode_hash(struct inode *inode) |
| 359 | { |
| 360 | spin_lock(&inode_hash_lock); |
| 361 | spin_lock(&inode->i_lock); |
| 362 | hlist_del_init(&inode->i_hash); |
| 363 | spin_unlock(&inode->i_lock); |
| 364 | spin_unlock(&inode_hash_lock); |
| 365 | } |
| 366 | EXPORT_SYMBOL(__remove_inode_hash); |
| 367 | |
| 368 | void end_writeback(struct inode *inode) |
| 369 | { |
| 370 | might_sleep(); |
| 371 | spin_lock_irq(&inode->i_data.tree_lock); |
| 372 | BUG_ON(inode->i_data.nrpages); |
| 373 | spin_unlock_irq(&inode->i_data.tree_lock); |
| 374 | BUG_ON(!list_empty(&inode->i_data.private_list)); |
| 375 | BUG_ON(!(inode->i_state & I_FREEING)); |
| 376 | BUG_ON(inode->i_state & I_CLEAR); |
| 377 | inode_sync_wait(inode); |
| 378 | |
| 379 | inode->i_state = I_FREEING | I_CLEAR; |
| 380 | } |
| 381 | EXPORT_SYMBOL(end_writeback); |
| 382 | |
| 383 | static void evict(struct inode *inode) |
| 384 | { |
| 385 | const struct super_operations *op = inode->i_sb->s_op; |
| 386 | |
| 387 | BUG_ON(!(inode->i_state & I_FREEING)); |
| 388 | BUG_ON(!list_empty(&inode->i_lru)); |
| 389 | |
| 390 | if (!list_empty(&inode->i_wb_list)) |
| 391 | inode_wb_list_del(inode); |
| 392 | |
| 393 | inode_sb_list_del(inode); |
| 394 | |
| 395 | if (op->evict_inode) { |
| 396 | op->evict_inode(inode); |
| 397 | } else { |
| 398 | if (inode->i_data.nrpages) |
| 399 | truncate_inode_pages(&inode->i_data, 0); |
| 400 | end_writeback(inode); |
| 401 | } |
| 402 | if (S_ISBLK(inode->i_mode) && inode->i_bdev) |
| 403 | bd_forget(inode); |
| 404 | if (S_ISCHR(inode->i_mode) && inode->i_cdev) |
| 405 | cd_forget(inode); |
| 406 | |
| 407 | remove_inode_hash(inode); |
| 408 | |
| 409 | spin_lock(&inode->i_lock); |
| 410 | wake_up_bit(&inode->i_state, __I_NEW); |
| 411 | BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); |
| 412 | spin_unlock(&inode->i_lock); |
| 413 | |
| 414 | destroy_inode(inode); |
| 415 | } |
| 416 | |
| 417 | static void dispose_list(struct list_head *head) |
| 418 | { |
| 419 | while (!list_empty(head)) { |
| 420 | struct inode *inode; |
| 421 | |
| 422 | inode = list_first_entry(head, struct inode, i_lru); |
| 423 | list_del_init(&inode->i_lru); |
| 424 | |
| 425 | evict(inode); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void evict_inodes(struct super_block *sb) |
| 430 | { |
| 431 | struct inode *inode, *next; |
| 432 | LIST_HEAD(dispose); |
| 433 | |
| 434 | spin_lock(&inode_sb_list_lock); |
| 435 | list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { |
| 436 | if (atomic_read(&inode->i_count)) |
| 437 | continue; |
| 438 | |
| 439 | spin_lock(&inode->i_lock); |
| 440 | if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { |
| 441 | spin_unlock(&inode->i_lock); |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | inode->i_state |= I_FREEING; |
| 446 | inode_lru_list_del(inode); |
| 447 | spin_unlock(&inode->i_lock); |
| 448 | list_add(&inode->i_lru, &dispose); |
| 449 | } |
| 450 | spin_unlock(&inode_sb_list_lock); |
| 451 | |
| 452 | dispose_list(&dispose); |
| 453 | } |
| 454 | |
| 455 | int invalidate_inodes(struct super_block *sb, bool kill_dirty) |
| 456 | { |
| 457 | int busy = 0; |
| 458 | struct inode *inode, *next; |
| 459 | LIST_HEAD(dispose); |
| 460 | |
| 461 | spin_lock(&inode_sb_list_lock); |
| 462 | list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) { |
| 463 | spin_lock(&inode->i_lock); |
| 464 | if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) { |
| 465 | spin_unlock(&inode->i_lock); |
| 466 | continue; |
| 467 | } |
| 468 | if (inode->i_state & I_DIRTY && !kill_dirty) { |
| 469 | spin_unlock(&inode->i_lock); |
| 470 | busy = 1; |
| 471 | continue; |
| 472 | } |
| 473 | if (atomic_read(&inode->i_count)) { |
| 474 | spin_unlock(&inode->i_lock); |
| 475 | busy = 1; |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | inode->i_state |= I_FREEING; |
| 480 | inode_lru_list_del(inode); |
| 481 | spin_unlock(&inode->i_lock); |
| 482 | list_add(&inode->i_lru, &dispose); |
| 483 | } |
| 484 | spin_unlock(&inode_sb_list_lock); |
| 485 | |
| 486 | dispose_list(&dispose); |
| 487 | |
| 488 | return busy; |
| 489 | } |
| 490 | |
| 491 | static int can_unuse(struct inode *inode) |
| 492 | { |
| 493 | if (inode->i_state & ~I_REFERENCED) |
| 494 | return 0; |
| 495 | if (inode_has_buffers(inode)) |
| 496 | return 0; |
| 497 | if (atomic_read(&inode->i_count)) |
| 498 | return 0; |
| 499 | if (inode->i_data.nrpages) |
| 500 | return 0; |
| 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | void prune_icache_sb(struct super_block *sb, int nr_to_scan) |
| 505 | { |
| 506 | LIST_HEAD(freeable); |
| 507 | int nr_scanned; |
| 508 | unsigned long reap = 0; |
| 509 | |
| 510 | spin_lock(&sb->s_inode_lru_lock); |
| 511 | for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) { |
| 512 | struct inode *inode; |
| 513 | |
| 514 | if (list_empty(&sb->s_inode_lru)) |
| 515 | break; |
| 516 | |
| 517 | inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru); |
| 518 | |
| 519 | if (!spin_trylock(&inode->i_lock)) { |
| 520 | list_move_tail(&inode->i_lru, &sb->s_inode_lru); |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | if (atomic_read(&inode->i_count) || |
| 525 | (inode->i_state & ~I_REFERENCED)) { |
| 526 | list_del_init(&inode->i_lru); |
| 527 | spin_unlock(&inode->i_lock); |
| 528 | sb->s_nr_inodes_unused--; |
| 529 | this_cpu_dec(nr_unused); |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | if (inode->i_state & I_REFERENCED) { |
| 535 | inode->i_state &= ~I_REFERENCED; |
| 536 | list_move(&inode->i_lru, &sb->s_inode_lru); |
| 537 | spin_unlock(&inode->i_lock); |
| 538 | continue; |
| 539 | } |
| 540 | if (inode_has_buffers(inode) || inode->i_data.nrpages) { |
| 541 | __iget(inode); |
| 542 | spin_unlock(&inode->i_lock); |
| 543 | spin_unlock(&sb->s_inode_lru_lock); |
| 544 | if (remove_inode_buffers(inode)) |
| 545 | reap += invalidate_mapping_pages(&inode->i_data, |
| 546 | 0, -1); |
| 547 | iput(inode); |
| 548 | spin_lock(&sb->s_inode_lru_lock); |
| 549 | |
| 550 | if (inode != list_entry(sb->s_inode_lru.next, |
| 551 | struct inode, i_lru)) |
| 552 | continue; |
| 553 | |
| 554 | if (!spin_trylock(&inode->i_lock)) |
| 555 | continue; |
| 556 | if (!can_unuse(inode)) { |
| 557 | spin_unlock(&inode->i_lock); |
| 558 | continue; |
| 559 | } |
| 560 | } |
| 561 | WARN_ON(inode->i_state & I_NEW); |
| 562 | inode->i_state |= I_FREEING; |
| 563 | spin_unlock(&inode->i_lock); |
| 564 | |
| 565 | list_move(&inode->i_lru, &freeable); |
| 566 | sb->s_nr_inodes_unused--; |
| 567 | this_cpu_dec(nr_unused); |
| 568 | } |
| 569 | if (current_is_kswapd()) |
| 570 | __count_vm_events(KSWAPD_INODESTEAL, reap); |
| 571 | else |
| 572 | __count_vm_events(PGINODESTEAL, reap); |
| 573 | spin_unlock(&sb->s_inode_lru_lock); |
| 574 | if (current->reclaim_state) |
| 575 | current->reclaim_state->reclaimed_slab += reap; |
| 576 | |
| 577 | dispose_list(&freeable); |
| 578 | } |
| 579 | |
| 580 | static void __wait_on_freeing_inode(struct inode *inode); |
| 581 | static struct inode *find_inode(struct super_block *sb, |
| 582 | struct hlist_head *head, |
| 583 | int (*test)(struct inode *, void *), |
| 584 | void *data) |
| 585 | { |
| 586 | struct hlist_node *node; |
| 587 | struct inode *inode = NULL; |
| 588 | |
| 589 | repeat: |
| 590 | hlist_for_each_entry(inode, node, head, i_hash) { |
| 591 | spin_lock(&inode->i_lock); |
| 592 | if (inode->i_sb != sb) { |
| 593 | spin_unlock(&inode->i_lock); |
| 594 | continue; |
| 595 | } |
| 596 | if (!test(inode, data)) { |
| 597 | spin_unlock(&inode->i_lock); |
| 598 | continue; |
| 599 | } |
| 600 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) { |
| 601 | __wait_on_freeing_inode(inode); |
| 602 | goto repeat; |
| 603 | } |
| 604 | __iget(inode); |
| 605 | spin_unlock(&inode->i_lock); |
| 606 | return inode; |
| 607 | } |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | static struct inode *find_inode_fast(struct super_block *sb, |
| 612 | struct hlist_head *head, unsigned long ino) |
| 613 | { |
| 614 | struct hlist_node *node; |
| 615 | struct inode *inode = NULL; |
| 616 | |
| 617 | repeat: |
| 618 | hlist_for_each_entry(inode, node, head, i_hash) { |
| 619 | spin_lock(&inode->i_lock); |
| 620 | if (inode->i_ino != ino) { |
| 621 | spin_unlock(&inode->i_lock); |
| 622 | continue; |
| 623 | } |
| 624 | if (inode->i_sb != sb) { |
| 625 | spin_unlock(&inode->i_lock); |
| 626 | continue; |
| 627 | } |
| 628 | if (inode->i_state & (I_FREEING|I_WILL_FREE)) { |
| 629 | __wait_on_freeing_inode(inode); |
| 630 | goto repeat; |
| 631 | } |
| 632 | __iget(inode); |
| 633 | spin_unlock(&inode->i_lock); |
| 634 | return inode; |
| 635 | } |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | #define LAST_INO_BATCH 1024 |
| 640 | static DEFINE_PER_CPU(unsigned int, last_ino); |
| 641 | |
| 642 | unsigned int get_next_ino(void) |
| 643 | { |
| 644 | unsigned int *p = &get_cpu_var(last_ino); |
| 645 | unsigned int res = *p; |
| 646 | |
| 647 | #ifdef CONFIG_SMP |
| 648 | if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) { |
| 649 | static atomic_t shared_last_ino; |
| 650 | int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino); |
| 651 | |
| 652 | res = next - LAST_INO_BATCH; |
| 653 | } |
| 654 | #endif |
| 655 | |
| 656 | *p = ++res; |
| 657 | put_cpu_var(last_ino); |
| 658 | return res; |
| 659 | } |
| 660 | EXPORT_SYMBOL(get_next_ino); |
| 661 | |
| 662 | struct inode *new_inode_pseudo(struct super_block *sb) |
| 663 | { |
| 664 | struct inode *inode = alloc_inode(sb); |
| 665 | |
| 666 | if (inode) { |
| 667 | spin_lock(&inode->i_lock); |
| 668 | inode->i_state = 0; |
| 669 | spin_unlock(&inode->i_lock); |
| 670 | INIT_LIST_HEAD(&inode->i_sb_list); |
| 671 | } |
| 672 | return inode; |
| 673 | } |
| 674 | |
| 675 | struct inode *new_inode(struct super_block *sb) |
| 676 | { |
| 677 | struct inode *inode; |
| 678 | |
| 679 | spin_lock_prefetch(&inode_sb_list_lock); |
| 680 | |
| 681 | inode = new_inode_pseudo(sb); |
| 682 | if (inode) |
| 683 | inode_sb_list_add(inode); |
| 684 | return inode; |
| 685 | } |
| 686 | EXPORT_SYMBOL(new_inode); |
| 687 | |
| 688 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 689 | void lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 690 | { |
| 691 | if (S_ISDIR(inode->i_mode)) { |
| 692 | struct file_system_type *type = inode->i_sb->s_type; |
| 693 | |
| 694 | |
| 695 | if (lockdep_match_class(&inode->i_mutex, &type->i_mutex_key)) { |
| 696 | mutex_destroy(&inode->i_mutex); |
| 697 | mutex_init(&inode->i_mutex); |
| 698 | lockdep_set_class(&inode->i_mutex, |
| 699 | &type->i_mutex_dir_key); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key); |
| 704 | #endif |
| 705 | |
| 706 | void unlock_new_inode(struct inode *inode) |
| 707 | { |
| 708 | lockdep_annotate_inode_mutex_key(inode); |
| 709 | spin_lock(&inode->i_lock); |
| 710 | WARN_ON(!(inode->i_state & I_NEW)); |
| 711 | inode->i_state &= ~I_NEW; |
| 712 | smp_mb(); |
| 713 | wake_up_bit(&inode->i_state, __I_NEW); |
| 714 | spin_unlock(&inode->i_lock); |
| 715 | } |
| 716 | EXPORT_SYMBOL(unlock_new_inode); |
| 717 | |
| 718 | struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, |
| 719 | int (*test)(struct inode *, void *), |
| 720 | int (*set)(struct inode *, void *), void *data) |
| 721 | { |
| 722 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 723 | struct inode *inode; |
| 724 | |
| 725 | spin_lock(&inode_hash_lock); |
| 726 | inode = find_inode(sb, head, test, data); |
| 727 | spin_unlock(&inode_hash_lock); |
| 728 | |
| 729 | if (inode) { |
| 730 | wait_on_inode(inode); |
| 731 | return inode; |
| 732 | } |
| 733 | |
| 734 | inode = alloc_inode(sb); |
| 735 | if (inode) { |
| 736 | struct inode *old; |
| 737 | |
| 738 | spin_lock(&inode_hash_lock); |
| 739 | |
| 740 | old = find_inode(sb, head, test, data); |
| 741 | if (!old) { |
| 742 | if (set(inode, data)) |
| 743 | goto set_failed; |
| 744 | |
| 745 | spin_lock(&inode->i_lock); |
| 746 | inode->i_state = I_NEW; |
| 747 | hlist_add_head(&inode->i_hash, head); |
| 748 | spin_unlock(&inode->i_lock); |
| 749 | inode_sb_list_add(inode); |
| 750 | spin_unlock(&inode_hash_lock); |
| 751 | |
| 752 | return inode; |
| 753 | } |
| 754 | |
| 755 | spin_unlock(&inode_hash_lock); |
| 756 | destroy_inode(inode); |
| 757 | inode = old; |
| 758 | wait_on_inode(inode); |
| 759 | } |
| 760 | return inode; |
| 761 | |
| 762 | set_failed: |
| 763 | spin_unlock(&inode_hash_lock); |
| 764 | destroy_inode(inode); |
| 765 | return NULL; |
| 766 | } |
| 767 | EXPORT_SYMBOL(iget5_locked); |
| 768 | |
| 769 | struct inode *iget_locked(struct super_block *sb, unsigned long ino) |
| 770 | { |
| 771 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
| 772 | struct inode *inode; |
| 773 | |
| 774 | spin_lock(&inode_hash_lock); |
| 775 | inode = find_inode_fast(sb, head, ino); |
| 776 | spin_unlock(&inode_hash_lock); |
| 777 | if (inode) { |
| 778 | wait_on_inode(inode); |
| 779 | return inode; |
| 780 | } |
| 781 | |
| 782 | inode = alloc_inode(sb); |
| 783 | if (inode) { |
| 784 | struct inode *old; |
| 785 | |
| 786 | spin_lock(&inode_hash_lock); |
| 787 | |
| 788 | old = find_inode_fast(sb, head, ino); |
| 789 | if (!old) { |
| 790 | inode->i_ino = ino; |
| 791 | spin_lock(&inode->i_lock); |
| 792 | inode->i_state = I_NEW; |
| 793 | hlist_add_head(&inode->i_hash, head); |
| 794 | spin_unlock(&inode->i_lock); |
| 795 | inode_sb_list_add(inode); |
| 796 | spin_unlock(&inode_hash_lock); |
| 797 | |
| 798 | return inode; |
| 799 | } |
| 800 | |
| 801 | spin_unlock(&inode_hash_lock); |
| 802 | destroy_inode(inode); |
| 803 | inode = old; |
| 804 | wait_on_inode(inode); |
| 805 | } |
| 806 | return inode; |
| 807 | } |
| 808 | EXPORT_SYMBOL(iget_locked); |
| 809 | |
| 810 | static int test_inode_iunique(struct super_block *sb, unsigned long ino) |
| 811 | { |
| 812 | struct hlist_head *b = inode_hashtable + hash(sb, ino); |
| 813 | struct hlist_node *node; |
| 814 | struct inode *inode; |
| 815 | |
| 816 | spin_lock(&inode_hash_lock); |
| 817 | hlist_for_each_entry(inode, node, b, i_hash) { |
| 818 | if (inode->i_ino == ino && inode->i_sb == sb) { |
| 819 | spin_unlock(&inode_hash_lock); |
| 820 | return 0; |
| 821 | } |
| 822 | } |
| 823 | spin_unlock(&inode_hash_lock); |
| 824 | |
| 825 | return 1; |
| 826 | } |
| 827 | |
| 828 | ino_t iunique(struct super_block *sb, ino_t max_reserved) |
| 829 | { |
| 830 | static DEFINE_SPINLOCK(iunique_lock); |
| 831 | static unsigned int counter; |
| 832 | ino_t res; |
| 833 | |
| 834 | spin_lock(&iunique_lock); |
| 835 | do { |
| 836 | if (counter <= max_reserved) |
| 837 | counter = max_reserved + 1; |
| 838 | res = counter++; |
| 839 | } while (!test_inode_iunique(sb, res)); |
| 840 | spin_unlock(&iunique_lock); |
| 841 | |
| 842 | return res; |
| 843 | } |
| 844 | EXPORT_SYMBOL(iunique); |
| 845 | |
| 846 | struct inode *igrab(struct inode *inode) |
| 847 | { |
| 848 | spin_lock(&inode->i_lock); |
| 849 | if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) { |
| 850 | __iget(inode); |
| 851 | spin_unlock(&inode->i_lock); |
| 852 | } else { |
| 853 | spin_unlock(&inode->i_lock); |
| 854 | inode = NULL; |
| 855 | } |
| 856 | return inode; |
| 857 | } |
| 858 | EXPORT_SYMBOL(igrab); |
| 859 | |
| 860 | struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, |
| 861 | int (*test)(struct inode *, void *), void *data) |
| 862 | { |
| 863 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 864 | struct inode *inode; |
| 865 | |
| 866 | spin_lock(&inode_hash_lock); |
| 867 | inode = find_inode(sb, head, test, data); |
| 868 | spin_unlock(&inode_hash_lock); |
| 869 | |
| 870 | return inode; |
| 871 | } |
| 872 | EXPORT_SYMBOL(ilookup5_nowait); |
| 873 | |
| 874 | struct inode *ilookup5(struct super_block *sb, unsigned long hashval, |
| 875 | int (*test)(struct inode *, void *), void *data) |
| 876 | { |
| 877 | struct inode *inode = ilookup5_nowait(sb, hashval, test, data); |
| 878 | |
| 879 | if (inode) |
| 880 | wait_on_inode(inode); |
| 881 | return inode; |
| 882 | } |
| 883 | EXPORT_SYMBOL(ilookup5); |
| 884 | |
| 885 | struct inode *ilookup(struct super_block *sb, unsigned long ino) |
| 886 | { |
| 887 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
| 888 | struct inode *inode; |
| 889 | |
| 890 | spin_lock(&inode_hash_lock); |
| 891 | inode = find_inode_fast(sb, head, ino); |
| 892 | spin_unlock(&inode_hash_lock); |
| 893 | |
| 894 | if (inode) |
| 895 | wait_on_inode(inode); |
| 896 | return inode; |
| 897 | } |
| 898 | EXPORT_SYMBOL(ilookup); |
| 899 | |
| 900 | int insert_inode_locked(struct inode *inode) |
| 901 | { |
| 902 | struct super_block *sb = inode->i_sb; |
| 903 | ino_t ino = inode->i_ino; |
| 904 | struct hlist_head *head = inode_hashtable + hash(sb, ino); |
| 905 | |
| 906 | while (1) { |
| 907 | struct hlist_node *node; |
| 908 | struct inode *old = NULL; |
| 909 | spin_lock(&inode_hash_lock); |
| 910 | hlist_for_each_entry(old, node, head, i_hash) { |
| 911 | if (old->i_ino != ino) |
| 912 | continue; |
| 913 | if (old->i_sb != sb) |
| 914 | continue; |
| 915 | spin_lock(&old->i_lock); |
| 916 | if (old->i_state & (I_FREEING|I_WILL_FREE)) { |
| 917 | spin_unlock(&old->i_lock); |
| 918 | continue; |
| 919 | } |
| 920 | break; |
| 921 | } |
| 922 | if (likely(!node)) { |
| 923 | spin_lock(&inode->i_lock); |
| 924 | inode->i_state |= I_NEW; |
| 925 | hlist_add_head(&inode->i_hash, head); |
| 926 | spin_unlock(&inode->i_lock); |
| 927 | spin_unlock(&inode_hash_lock); |
| 928 | return 0; |
| 929 | } |
| 930 | __iget(old); |
| 931 | spin_unlock(&old->i_lock); |
| 932 | spin_unlock(&inode_hash_lock); |
| 933 | wait_on_inode(old); |
| 934 | if (unlikely(!inode_unhashed(old))) { |
| 935 | iput(old); |
| 936 | return -EBUSY; |
| 937 | } |
| 938 | iput(old); |
| 939 | } |
| 940 | } |
| 941 | EXPORT_SYMBOL(insert_inode_locked); |
| 942 | |
| 943 | int insert_inode_locked4(struct inode *inode, unsigned long hashval, |
| 944 | int (*test)(struct inode *, void *), void *data) |
| 945 | { |
| 946 | struct super_block *sb = inode->i_sb; |
| 947 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); |
| 948 | |
| 949 | while (1) { |
| 950 | struct hlist_node *node; |
| 951 | struct inode *old = NULL; |
| 952 | |
| 953 | spin_lock(&inode_hash_lock); |
| 954 | hlist_for_each_entry(old, node, head, i_hash) { |
| 955 | if (old->i_sb != sb) |
| 956 | continue; |
| 957 | if (!test(old, data)) |
| 958 | continue; |
| 959 | spin_lock(&old->i_lock); |
| 960 | if (old->i_state & (I_FREEING|I_WILL_FREE)) { |
| 961 | spin_unlock(&old->i_lock); |
| 962 | continue; |
| 963 | } |
| 964 | break; |
| 965 | } |
| 966 | if (likely(!node)) { |
| 967 | spin_lock(&inode->i_lock); |
| 968 | inode->i_state |= I_NEW; |
| 969 | hlist_add_head(&inode->i_hash, head); |
| 970 | spin_unlock(&inode->i_lock); |
| 971 | spin_unlock(&inode_hash_lock); |
| 972 | return 0; |
| 973 | } |
| 974 | __iget(old); |
| 975 | spin_unlock(&old->i_lock); |
| 976 | spin_unlock(&inode_hash_lock); |
| 977 | wait_on_inode(old); |
| 978 | if (unlikely(!inode_unhashed(old))) { |
| 979 | iput(old); |
| 980 | return -EBUSY; |
| 981 | } |
| 982 | iput(old); |
| 983 | } |
| 984 | } |
| 985 | EXPORT_SYMBOL(insert_inode_locked4); |
| 986 | |
| 987 | |
| 988 | int generic_delete_inode(struct inode *inode) |
| 989 | { |
| 990 | return 1; |
| 991 | } |
| 992 | EXPORT_SYMBOL(generic_delete_inode); |
| 993 | |
| 994 | static void iput_final(struct inode *inode) |
| 995 | { |
| 996 | struct super_block *sb = inode->i_sb; |
| 997 | const struct super_operations *op = inode->i_sb->s_op; |
| 998 | int drop; |
| 999 | |
| 1000 | WARN_ON(inode->i_state & I_NEW); |
| 1001 | |
| 1002 | if (op->drop_inode) |
| 1003 | drop = op->drop_inode(inode); |
| 1004 | else |
| 1005 | drop = generic_drop_inode(inode); |
| 1006 | |
| 1007 | if (!drop && (sb->s_flags & MS_ACTIVE)) { |
| 1008 | inode->i_state |= I_REFERENCED; |
| 1009 | if (!(inode->i_state & (I_DIRTY|I_SYNC))) |
| 1010 | inode_lru_list_add(inode); |
| 1011 | spin_unlock(&inode->i_lock); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | if (!drop) { |
| 1016 | inode->i_state |= I_WILL_FREE; |
| 1017 | spin_unlock(&inode->i_lock); |
| 1018 | write_inode_now(inode, 1); |
| 1019 | spin_lock(&inode->i_lock); |
| 1020 | WARN_ON(inode->i_state & I_NEW); |
| 1021 | inode->i_state &= ~I_WILL_FREE; |
| 1022 | } |
| 1023 | |
| 1024 | inode->i_state |= I_FREEING; |
| 1025 | if (!list_empty(&inode->i_lru)) |
| 1026 | inode_lru_list_del(inode); |
| 1027 | spin_unlock(&inode->i_lock); |
| 1028 | |
| 1029 | evict(inode); |
| 1030 | } |
| 1031 | |
| 1032 | void iput(struct inode *inode) |
| 1033 | { |
| 1034 | if (inode) { |
| 1035 | BUG_ON(inode->i_state & I_CLEAR); |
| 1036 | |
| 1037 | if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) |
| 1038 | iput_final(inode); |
| 1039 | } |
| 1040 | } |
| 1041 | EXPORT_SYMBOL(iput); |
| 1042 | |
| 1043 | sector_t bmap(struct inode *inode, sector_t block) |
| 1044 | { |
| 1045 | sector_t res = 0; |
| 1046 | if (inode->i_mapping->a_ops->bmap) |
| 1047 | res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); |
| 1048 | return res; |
| 1049 | } |
| 1050 | EXPORT_SYMBOL(bmap); |
| 1051 | |
| 1052 | static int relatime_need_update(struct vfsmount *mnt, struct inode *inode, |
| 1053 | struct timespec now) |
| 1054 | { |
| 1055 | |
| 1056 | if (!(mnt->mnt_flags & MNT_RELATIME)) |
| 1057 | return 1; |
| 1058 | if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0) |
| 1059 | return 1; |
| 1060 | if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0) |
| 1061 | return 1; |
| 1062 | |
| 1063 | if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60) |
| 1064 | return 1; |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | void touch_atime(struct path *path) |
| 1069 | { |
| 1070 | struct vfsmount *mnt = path->mnt; |
| 1071 | struct inode *inode = path->dentry->d_inode; |
| 1072 | struct timespec now; |
| 1073 | |
| 1074 | if (inode->i_flags & S_NOATIME) |
| 1075 | return; |
| 1076 | if (IS_NOATIME(inode)) |
| 1077 | return; |
| 1078 | if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)) |
| 1079 | return; |
| 1080 | |
| 1081 | if (mnt->mnt_flags & MNT_NOATIME) |
| 1082 | return; |
| 1083 | if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) |
| 1084 | return; |
| 1085 | |
| 1086 | now = current_fs_time(inode->i_sb); |
| 1087 | |
| 1088 | if (!relatime_need_update(mnt, inode, now)) |
| 1089 | return; |
| 1090 | |
| 1091 | if (timespec_equal(&inode->i_atime, &now)) |
| 1092 | return; |
| 1093 | |
| 1094 | if (mnt_want_write(mnt)) |
| 1095 | return; |
| 1096 | |
| 1097 | inode->i_atime = now; |
| 1098 | mark_inode_dirty_sync(inode); |
| 1099 | mnt_drop_write(mnt); |
| 1100 | } |
| 1101 | EXPORT_SYMBOL(touch_atime); |
| 1102 | |
| 1103 | |
| 1104 | void file_update_time(struct file *file) |
| 1105 | { |
| 1106 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1107 | struct timespec now; |
| 1108 | enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0; |
| 1109 | |
| 1110 | |
| 1111 | if (IS_NOCMTIME(inode)) |
| 1112 | return; |
| 1113 | |
| 1114 | now = current_fs_time(inode->i_sb); |
| 1115 | if (!timespec_equal(&inode->i_mtime, &now)) |
| 1116 | sync_it = S_MTIME; |
| 1117 | |
| 1118 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 1119 | sync_it |= S_CTIME; |
| 1120 | |
| 1121 | if (IS_I_VERSION(inode)) |
| 1122 | sync_it |= S_VERSION; |
| 1123 | |
| 1124 | if (!sync_it) |
| 1125 | return; |
| 1126 | |
| 1127 | |
| 1128 | if (mnt_want_write_file(file)) |
| 1129 | return; |
| 1130 | |
| 1131 | |
| 1132 | if (sync_it & S_VERSION) |
| 1133 | inode_inc_iversion(inode); |
| 1134 | if (sync_it & S_CTIME) |
| 1135 | inode->i_ctime = now; |
| 1136 | if (sync_it & S_MTIME) |
| 1137 | inode->i_mtime = now; |
| 1138 | mark_inode_dirty_sync(inode); |
| 1139 | mnt_drop_write_file(file); |
| 1140 | } |
| 1141 | EXPORT_SYMBOL(file_update_time); |
| 1142 | |
| 1143 | int inode_needs_sync(struct inode *inode) |
| 1144 | { |
| 1145 | if (IS_SYNC(inode)) |
| 1146 | return 1; |
| 1147 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) |
| 1148 | return 1; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | EXPORT_SYMBOL(inode_needs_sync); |
| 1152 | |
| 1153 | int inode_wait(void *word) |
| 1154 | { |
| 1155 | schedule(); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | EXPORT_SYMBOL(inode_wait); |
| 1159 | |
| 1160 | static void __wait_on_freeing_inode(struct inode *inode) |
| 1161 | { |
| 1162 | wait_queue_head_t *wq; |
| 1163 | DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW); |
| 1164 | wq = bit_waitqueue(&inode->i_state, __I_NEW); |
| 1165 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 1166 | spin_unlock(&inode->i_lock); |
| 1167 | spin_unlock(&inode_hash_lock); |
| 1168 | schedule(); |
| 1169 | finish_wait(wq, &wait.wait); |
| 1170 | spin_lock(&inode_hash_lock); |
| 1171 | } |
| 1172 | |
| 1173 | static __initdata unsigned long ihash_entries; |
| 1174 | static int __init set_ihash_entries(char *str) |
| 1175 | { |
| 1176 | if (!str) |
| 1177 | return 0; |
| 1178 | ihash_entries = simple_strtoul(str, &str, 0); |
| 1179 | return 1; |
| 1180 | } |
| 1181 | __setup("ihash_entries=", set_ihash_entries); |
| 1182 | |
| 1183 | void __init inode_init_early(void) |
| 1184 | { |
| 1185 | unsigned int loop; |
| 1186 | |
| 1187 | if (hashdist) |
| 1188 | return; |
| 1189 | |
| 1190 | inode_hashtable = |
| 1191 | alloc_large_system_hash("Inode-cache", |
| 1192 | sizeof(struct hlist_head), |
| 1193 | ihash_entries, |
| 1194 | 14, |
| 1195 | HASH_EARLY, |
| 1196 | &i_hash_shift, |
| 1197 | &i_hash_mask, |
| 1198 | 0); |
| 1199 | |
| 1200 | for (loop = 0; loop < (1U << i_hash_shift); loop++) |
| 1201 | INIT_HLIST_HEAD(&inode_hashtable[loop]); |
| 1202 | } |
| 1203 | |
| 1204 | void __init inode_init(void) |
| 1205 | { |
| 1206 | unsigned int loop; |
| 1207 | |
| 1208 | |
| 1209 | inode_cachep = kmem_cache_create("inode_cache", |
| 1210 | sizeof(struct inode), |
| 1211 | 0, |
| 1212 | (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| |
| 1213 | SLAB_MEM_SPREAD), |
| 1214 | init_once); |
| 1215 | |
| 1216 | |
| 1217 | if (!hashdist) |
| 1218 | return; |
| 1219 | |
| 1220 | inode_hashtable = |
| 1221 | alloc_large_system_hash("Inode-cache", |
| 1222 | sizeof(struct hlist_head), |
| 1223 | ihash_entries, |
| 1224 | 14, |
| 1225 | 0, |
| 1226 | &i_hash_shift, |
| 1227 | &i_hash_mask, |
| 1228 | 0); |
| 1229 | |
| 1230 | for (loop = 0; loop < (1U << i_hash_shift); loop++) |
| 1231 | INIT_HLIST_HEAD(&inode_hashtable[loop]); |
| 1232 | } |
| 1233 | |
| 1234 | void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) |
| 1235 | { |
| 1236 | inode->i_mode = mode; |
| 1237 | if (S_ISCHR(mode)) { |
| 1238 | inode->i_fop = &def_chr_fops; |
| 1239 | inode->i_rdev = rdev; |
| 1240 | } else if (S_ISBLK(mode)) { |
| 1241 | inode->i_fop = &def_blk_fops; |
| 1242 | inode->i_rdev = rdev; |
| 1243 | } else if (S_ISFIFO(mode)) |
| 1244 | inode->i_fop = &def_fifo_fops; |
| 1245 | else if (S_ISSOCK(mode)) |
| 1246 | inode->i_fop = &bad_sock_fops; |
| 1247 | else |
| 1248 | printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for" |
| 1249 | " inode %s:%lu\n", mode, inode->i_sb->s_id, |
| 1250 | inode->i_ino); |
| 1251 | } |
| 1252 | EXPORT_SYMBOL(init_special_inode); |
| 1253 | |
| 1254 | void inode_init_owner(struct inode *inode, const struct inode *dir, |
| 1255 | umode_t mode) |
| 1256 | { |
| 1257 | inode->i_uid = current_fsuid(); |
| 1258 | if (dir && dir->i_mode & S_ISGID) { |
| 1259 | inode->i_gid = dir->i_gid; |
| 1260 | if (S_ISDIR(mode)) |
| 1261 | mode |= S_ISGID; |
| 1262 | } else |
| 1263 | inode->i_gid = current_fsgid(); |
| 1264 | inode->i_mode = mode; |
| 1265 | } |
| 1266 | EXPORT_SYMBOL(inode_init_owner); |
| 1267 | |
| 1268 | bool inode_owner_or_capable(const struct inode *inode) |
| 1269 | { |
| 1270 | struct user_namespace *ns = inode_userns(inode); |
| 1271 | |
| 1272 | if (current_user_ns() == ns && current_fsuid() == inode->i_uid) |
| 1273 | return true; |
| 1274 | if (ns_capable(ns, CAP_FOWNER)) |
| 1275 | return true; |
| 1276 | if (current_user_ns() == ns && |
| 1277 | (inode->i_gid == AID_SDCARD_RW || inode->i_gid == AID_SDCARD_R)) |
| 1278 | return true; |
| 1279 | return false; |
| 1280 | } |
| 1281 | EXPORT_SYMBOL(inode_owner_or_capable); |