aboutsummaryrefslogtreecommitdiffstats
path: root/fs/namei.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/namei.c')
-rw-r--r--fs/namei.c221
1 files changed, 193 insertions, 28 deletions
diff --git a/fs/namei.c b/fs/namei.c
index 208c6aa4a989..a94a7f9a03ea 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -642,7 +642,7 @@ follow_link(struct path *link, struct nameidata *nd, void **p)
642 cond_resched(); 642 cond_resched();
643 current->total_link_count++; 643 current->total_link_count++;
644 644
645 touch_atime(link->mnt, dentry); 645 touch_atime(link);
646 nd_set_link(nd, NULL); 646 nd_set_link(nd, NULL);
647 647
648 error = security_inode_follow_link(link->dentry, nd); 648 error = security_inode_follow_link(link->dentry, nd);
@@ -1095,8 +1095,10 @@ static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentr
1095 struct dentry *old; 1095 struct dentry *old;
1096 1096
1097 /* Don't create child dentry for a dead directory. */ 1097 /* Don't create child dentry for a dead directory. */
1098 if (unlikely(IS_DEADDIR(inode))) 1098 if (unlikely(IS_DEADDIR(inode))) {
1099 dput(dentry);
1099 return ERR_PTR(-ENOENT); 1100 return ERR_PTR(-ENOENT);
1101 }
1100 1102
1101 old = inode->i_op->lookup(inode, dentry, nd); 1103 old = inode->i_op->lookup(inode, dentry, nd);
1102 if (unlikely(old)) { 1104 if (unlikely(old)) {
@@ -1373,6 +1375,162 @@ static inline int can_lookup(struct inode *inode)
1373} 1375}
1374 1376
1375/* 1377/*
1378 * We can do the critical dentry name comparison and hashing
1379 * operations one word at a time, but we are limited to:
1380 *
1381 * - Architectures with fast unaligned word accesses. We could
1382 * do a "get_unaligned()" if this helps and is sufficiently
1383 * fast.
1384 *
1385 * - Little-endian machines (so that we can generate the mask
1386 * of low bytes efficiently). Again, we *could* do a byte
1387 * swapping load on big-endian architectures if that is not
1388 * expensive enough to make the optimization worthless.
1389 *
1390 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1391 * do not trap on the (extremely unlikely) case of a page
1392 * crossing operation.
1393 *
1394 * - Furthermore, we need an efficient 64-bit compile for the
1395 * 64-bit case in order to generate the "number of bytes in
1396 * the final mask". Again, that could be replaced with a
1397 * efficient population count instruction or similar.
1398 */
1399#ifdef CONFIG_DCACHE_WORD_ACCESS
1400
1401#ifdef CONFIG_64BIT
1402
1403/*
1404 * Jan Achrenius on G+: microoptimized version of
1405 * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
1406 * that works for the bytemasks without having to
1407 * mask them first.
1408 */
1409static inline long count_masked_bytes(unsigned long mask)
1410{
1411 return mask*0x0001020304050608 >> 56;
1412}
1413
1414static inline unsigned int fold_hash(unsigned long hash)
1415{
1416 hash += hash >> (8*sizeof(int));
1417 return hash;
1418}
1419
1420#else /* 32-bit case */
1421
1422/* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
1423static inline long count_masked_bytes(long mask)
1424{
1425 /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
1426 long a = (0x0ff0001+mask) >> 23;
1427 /* Fix the 1 for 00 case */
1428 return a & mask;
1429}
1430
1431#define fold_hash(x) (x)
1432
1433#endif
1434
1435unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1436{
1437 unsigned long a, mask;
1438 unsigned long hash = 0;
1439
1440 for (;;) {
1441 a = *(unsigned long *)name;
1442 hash *= 9;
1443 if (len < sizeof(unsigned long))
1444 break;
1445 hash += a;
1446 name += sizeof(unsigned long);
1447 len -= sizeof(unsigned long);
1448 if (!len)
1449 goto done;
1450 }
1451 mask = ~(~0ul << len*8);
1452 hash += mask & a;
1453done:
1454 return fold_hash(hash);
1455}
1456EXPORT_SYMBOL(full_name_hash);
1457
1458#ifdef CONFIG_64BIT
1459#define ONEBYTES 0x0101010101010101ul
1460#define SLASHBYTES 0x2f2f2f2f2f2f2f2ful
1461#define HIGHBITS 0x8080808080808080ul
1462#else
1463#define ONEBYTES 0x01010101ul
1464#define SLASHBYTES 0x2f2f2f2ful
1465#define HIGHBITS 0x80808080ul
1466#endif
1467
1468/* Return the high bit set in the first byte that is a zero */
1469static inline unsigned long has_zero(unsigned long a)
1470{
1471 return ((a - ONEBYTES) & ~a) & HIGHBITS;
1472}
1473
1474/*
1475 * Calculate the length and hash of the path component, and
1476 * return the length of the component;
1477 */
1478static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1479{
1480 unsigned long a, mask, hash, len;
1481
1482 hash = a = 0;
1483 len = -sizeof(unsigned long);
1484 do {
1485 hash = (hash + a) * 9;
1486 len += sizeof(unsigned long);
1487 a = *(unsigned long *)(name+len);
1488 /* Do we have any NUL or '/' bytes in this word? */
1489 mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
1490 } while (!mask);
1491
1492 /* The mask *below* the first high bit set */
1493 mask = (mask - 1) & ~mask;
1494 mask >>= 7;
1495 hash += a & mask;
1496 *hashp = fold_hash(hash);
1497
1498 return len + count_masked_bytes(mask);
1499}
1500
1501#else
1502
1503unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1504{
1505 unsigned long hash = init_name_hash();
1506 while (len--)
1507 hash = partial_name_hash(*name++, hash);
1508 return end_name_hash(hash);
1509}
1510EXPORT_SYMBOL(full_name_hash);
1511
1512/*
1513 * We know there's a real path component here of at least
1514 * one character.
1515 */
1516static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1517{
1518 unsigned long hash = init_name_hash();
1519 unsigned long len = 0, c;
1520
1521 c = (unsigned char)*name;
1522 do {
1523 len++;
1524 hash = partial_name_hash(c, hash);
1525 c = (unsigned char)name[len];
1526 } while (c && c != '/');
1527 *hashp = end_name_hash(hash);
1528 return len;
1529}
1530
1531#endif
1532
1533/*
1376 * Name resolution. 1534 * Name resolution.
1377 * This is the basic name resolution function, turning a pathname into 1535 * This is the basic name resolution function, turning a pathname into
1378 * the final dentry. We expect 'base' to be positive and a directory. 1536 * the final dentry. We expect 'base' to be positive and a directory.
@@ -1392,31 +1550,22 @@ static int link_path_walk(const char *name, struct nameidata *nd)
1392 1550
1393 /* At this point we know we have a real path component. */ 1551 /* At this point we know we have a real path component. */
1394 for(;;) { 1552 for(;;) {
1395 unsigned long hash;
1396 struct qstr this; 1553 struct qstr this;
1397 unsigned int c; 1554 long len;
1398 int type; 1555 int type;
1399 1556
1400 err = may_lookup(nd); 1557 err = may_lookup(nd);
1401 if (err) 1558 if (err)
1402 break; 1559 break;
1403 1560
1561 len = hash_name(name, &this.hash);
1404 this.name = name; 1562 this.name = name;
1405 c = *(const unsigned char *)name; 1563 this.len = len;
1406
1407 hash = init_name_hash();
1408 do {
1409 name++;
1410 hash = partial_name_hash(c, hash);
1411 c = *(const unsigned char *)name;
1412 } while (c && (c != '/'));
1413 this.len = name - (const char *) this.name;
1414 this.hash = end_name_hash(hash);
1415 1564
1416 type = LAST_NORM; 1565 type = LAST_NORM;
1417 if (this.name[0] == '.') switch (this.len) { 1566 if (name[0] == '.') switch (len) {
1418 case 2: 1567 case 2:
1419 if (this.name[1] == '.') { 1568 if (name[1] == '.') {
1420 type = LAST_DOTDOT; 1569 type = LAST_DOTDOT;
1421 nd->flags |= LOOKUP_JUMPED; 1570 nd->flags |= LOOKUP_JUMPED;
1422 } 1571 }
@@ -1435,12 +1584,18 @@ static int link_path_walk(const char *name, struct nameidata *nd)
1435 } 1584 }
1436 } 1585 }
1437 1586
1438 /* remove trailing slashes? */ 1587 if (!name[len])
1439 if (!c)
1440 goto last_component; 1588 goto last_component;
1441 while (*++name == '/'); 1589 /*
1442 if (!*name) 1590 * If it wasn't NUL, we know it was '/'. Skip that
1591 * slash, and continue until no more slashes.
1592 */
1593 do {
1594 len++;
1595 } while (unlikely(name[len] == '/'));
1596 if (!name[len])
1443 goto last_component; 1597 goto last_component;
1598 name += len;
1444 1599
1445 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1600 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1446 if (err < 0) 1601 if (err < 0)
@@ -1773,24 +1928,21 @@ static struct dentry *lookup_hash(struct nameidata *nd)
1773struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1928struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1774{ 1929{
1775 struct qstr this; 1930 struct qstr this;
1776 unsigned long hash;
1777 unsigned int c; 1931 unsigned int c;
1778 1932
1779 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 1933 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1780 1934
1781 this.name = name; 1935 this.name = name;
1782 this.len = len; 1936 this.len = len;
1937 this.hash = full_name_hash(name, len);
1783 if (!len) 1938 if (!len)
1784 return ERR_PTR(-EACCES); 1939 return ERR_PTR(-EACCES);
1785 1940
1786 hash = init_name_hash();
1787 while (len--) { 1941 while (len--) {
1788 c = *(const unsigned char *)name++; 1942 c = *(const unsigned char *)name++;
1789 if (c == '/' || c == '\0') 1943 if (c == '/' || c == '\0')
1790 return ERR_PTR(-EACCES); 1944 return ERR_PTR(-EACCES);
1791 hash = partial_name_hash(c, hash);
1792 } 1945 }
1793 this.hash = end_name_hash(hash);
1794 /* 1946 /*
1795 * See if the low-level filesystem might want 1947 * See if the low-level filesystem might want
1796 * to use its own hash.. 1948 * to use its own hash..
@@ -2138,7 +2290,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
2138 /* sayonara */ 2290 /* sayonara */
2139 error = complete_walk(nd); 2291 error = complete_walk(nd);
2140 if (error) 2292 if (error)
2141 return ERR_PTR(-ECHILD); 2293 return ERR_PTR(error);
2142 2294
2143 error = -ENOTDIR; 2295 error = -ENOTDIR;
2144 if (nd->flags & LOOKUP_DIRECTORY) { 2296 if (nd->flags & LOOKUP_DIRECTORY) {
@@ -2237,7 +2389,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
2237 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2389 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2238 error = complete_walk(nd); 2390 error = complete_walk(nd);
2239 if (error) 2391 if (error)
2240 goto exit; 2392 return ERR_PTR(error);
2241 error = -EISDIR; 2393 error = -EISDIR;
2242 if (S_ISDIR(nd->inode->i_mode)) 2394 if (S_ISDIR(nd->inode->i_mode))
2243 goto exit; 2395 goto exit;
@@ -2545,6 +2697,7 @@ SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, d
2545int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 2697int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2546{ 2698{
2547 int error = may_create(dir, dentry); 2699 int error = may_create(dir, dentry);
2700 unsigned max_links = dir->i_sb->s_max_links;
2548 2701
2549 if (error) 2702 if (error)
2550 return error; 2703 return error;
@@ -2557,6 +2710,9 @@ int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2557 if (error) 2710 if (error)
2558 return error; 2711 return error;
2559 2712
2713 if (max_links && dir->i_nlink >= max_links)
2714 return -EMLINK;
2715
2560 error = dir->i_op->mkdir(dir, dentry, mode); 2716 error = dir->i_op->mkdir(dir, dentry, mode);
2561 if (!error) 2717 if (!error)
2562 fsnotify_mkdir(dir, dentry); 2718 fsnotify_mkdir(dir, dentry);
@@ -2887,6 +3043,7 @@ SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newn
2887int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 3043int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2888{ 3044{
2889 struct inode *inode = old_dentry->d_inode; 3045 struct inode *inode = old_dentry->d_inode;
3046 unsigned max_links = dir->i_sb->s_max_links;
2890 int error; 3047 int error;
2891 3048
2892 if (!inode) 3049 if (!inode)
@@ -2917,6 +3074,8 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
2917 /* Make sure we don't allow creating hardlink to an unlinked file */ 3074 /* Make sure we don't allow creating hardlink to an unlinked file */
2918 if (inode->i_nlink == 0) 3075 if (inode->i_nlink == 0)
2919 error = -ENOENT; 3076 error = -ENOENT;
3077 else if (max_links && inode->i_nlink >= max_links)
3078 error = -EMLINK;
2920 else 3079 else
2921 error = dir->i_op->link(old_dentry, dir, new_dentry); 3080 error = dir->i_op->link(old_dentry, dir, new_dentry);
2922 mutex_unlock(&inode->i_mutex); 3081 mutex_unlock(&inode->i_mutex);
@@ -3026,6 +3185,7 @@ static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3026{ 3185{
3027 int error = 0; 3186 int error = 0;
3028 struct inode *target = new_dentry->d_inode; 3187 struct inode *target = new_dentry->d_inode;
3188 unsigned max_links = new_dir->i_sb->s_max_links;
3029 3189
3030 /* 3190 /*
3031 * If we are going to change the parent - check write permissions, 3191 * If we are going to change the parent - check write permissions,
@@ -3049,6 +3209,11 @@ static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3049 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 3209 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3050 goto out; 3210 goto out;
3051 3211
3212 error = -EMLINK;
3213 if (max_links && !target && new_dir != old_dir &&
3214 new_dir->i_nlink >= max_links)
3215 goto out;
3216
3052 if (target) 3217 if (target)
3053 shrink_dcache_parent(new_dentry); 3218 shrink_dcache_parent(new_dentry);
3054 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3219 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
@@ -3347,9 +3512,9 @@ retry:
3347 if (err) 3512 if (err)
3348 goto fail; 3513 goto fail;
3349 3514
3350 kaddr = kmap_atomic(page, KM_USER0); 3515 kaddr = kmap_atomic(page);
3351 memcpy(kaddr, symname, len-1); 3516 memcpy(kaddr, symname, len-1);
3352 kunmap_atomic(kaddr, KM_USER0); 3517 kunmap_atomic(kaddr);
3353 3518
3354 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3519 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3355 page, fsdata); 3520 page, fsdata);