aboutsummaryrefslogtreecommitdiffstats
path: root/fs/cifs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/cifs/inode.c')
-rw-r--r--fs/cifs/inode.c338
1 files changed, 219 insertions, 119 deletions
diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c
index cababd8a52df..29b9ea244c81 100644
--- a/fs/cifs/inode.c
+++ b/fs/cifs/inode.c
@@ -20,6 +20,7 @@
20 */ 20 */
21#include <linux/fs.h> 21#include <linux/fs.h>
22#include <linux/stat.h> 22#include <linux/stat.h>
23#include <linux/slab.h>
23#include <linux/pagemap.h> 24#include <linux/pagemap.h>
24#include <asm/div64.h> 25#include <asm/div64.h>
25#include "cifsfs.h" 26#include "cifsfs.h"
@@ -77,6 +78,41 @@ static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
77 } 78 }
78} 79}
79 80
81/* check inode attributes against fattr. If they don't match, tag the
82 * inode for cache invalidation
83 */
84static void
85cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
86{
87 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
88
89 cFYI(1, ("%s: revalidating inode %llu", __func__, cifs_i->uniqueid));
90
91 if (inode->i_state & I_NEW) {
92 cFYI(1, ("%s: inode %llu is new", __func__, cifs_i->uniqueid));
93 return;
94 }
95
96 /* don't bother with revalidation if we have an oplock */
97 if (cifs_i->clientCanCacheRead) {
98 cFYI(1, ("%s: inode %llu is oplocked", __func__,
99 cifs_i->uniqueid));
100 return;
101 }
102
103 /* revalidate if mtime or size have changed */
104 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
105 cifs_i->server_eof == fattr->cf_eof) {
106 cFYI(1, ("%s: inode %llu is unchanged", __func__,
107 cifs_i->uniqueid));
108 return;
109 }
110
111 cFYI(1, ("%s: invalidating inode %llu mapping", __func__,
112 cifs_i->uniqueid));
113 cifs_i->invalid_mapping = true;
114}
115
80/* populate an inode with info from a cifs_fattr struct */ 116/* populate an inode with info from a cifs_fattr struct */
81void 117void
82cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) 118cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
@@ -85,6 +121,8 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
85 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 121 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
86 unsigned long oldtime = cifs_i->time; 122 unsigned long oldtime = cifs_i->time;
87 123
124 cifs_revalidate_cache(inode, fattr);
125
88 inode->i_atime = fattr->cf_atime; 126 inode->i_atime = fattr->cf_atime;
89 inode->i_mtime = fattr->cf_mtime; 127 inode->i_mtime = fattr->cf_mtime;
90 inode->i_ctime = fattr->cf_ctime; 128 inode->i_ctime = fattr->cf_ctime;
@@ -111,6 +149,7 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
111 149
112 cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING; 150 cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
113 151
152 cifs_i->server_eof = fattr->cf_eof;
114 /* 153 /*
115 * Can't safely change the file size here if the client is writing to 154 * Can't safely change the file size here if the client is writing to
116 * it due to potential races. 155 * it due to potential races.
@@ -230,6 +269,31 @@ cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
230 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL; 269 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
231} 270}
232 271
272int cifs_get_file_info_unix(struct file *filp)
273{
274 int rc;
275 int xid;
276 FILE_UNIX_BASIC_INFO find_data;
277 struct cifs_fattr fattr;
278 struct inode *inode = filp->f_path.dentry->d_inode;
279 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
280 struct cifsTconInfo *tcon = cifs_sb->tcon;
281 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
282
283 xid = GetXid();
284 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->netfid, &find_data);
285 if (!rc) {
286 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
287 } else if (rc == -EREMOTE) {
288 cifs_create_dfs_fattr(&fattr, inode->i_sb);
289 rc = 0;
290 }
291
292 cifs_fattr_to_inode(inode, &fattr);
293 FreeXid(xid);
294 return rc;
295}
296
233int cifs_get_inode_info_unix(struct inode **pinode, 297int cifs_get_inode_info_unix(struct inode **pinode,
234 const unsigned char *full_path, 298 const unsigned char *full_path,
235 struct super_block *sb, int xid) 299 struct super_block *sb, int xid)
@@ -366,7 +430,7 @@ static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
366 char ea_value[4]; 430 char ea_value[4];
367 __u32 mode; 431 __u32 mode;
368 432
369 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS", 433 rc = CIFSSMBQAllEAs(xid, cifs_sb->tcon, path, "SETFILEBITS",
370 ea_value, 4 /* size of buf */, cifs_sb->local_nls, 434 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
371 cifs_sb->mnt_cifs_flags & 435 cifs_sb->mnt_cifs_flags &
372 CIFS_MOUNT_MAP_SPECIAL_CHR); 436 CIFS_MOUNT_MAP_SPECIAL_CHR);
@@ -431,6 +495,47 @@ cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
431 fattr->cf_gid = cifs_sb->mnt_gid; 495 fattr->cf_gid = cifs_sb->mnt_gid;
432} 496}
433 497
498int cifs_get_file_info(struct file *filp)
499{
500 int rc;
501 int xid;
502 FILE_ALL_INFO find_data;
503 struct cifs_fattr fattr;
504 struct inode *inode = filp->f_path.dentry->d_inode;
505 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
506 struct cifsTconInfo *tcon = cifs_sb->tcon;
507 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
508
509 xid = GetXid();
510 rc = CIFSSMBQFileInfo(xid, tcon, cfile->netfid, &find_data);
511 if (rc == -EOPNOTSUPP || rc == -EINVAL) {
512 /*
513 * FIXME: legacy server -- fall back to path-based call?
514 * for now, just skip revalidating and mark inode for
515 * immediate reval.
516 */
517 rc = 0;
518 CIFS_I(inode)->time = 0;
519 goto cgfi_exit;
520 } else if (rc == -EREMOTE) {
521 cifs_create_dfs_fattr(&fattr, inode->i_sb);
522 rc = 0;
523 } else if (rc)
524 goto cgfi_exit;
525
526 /*
527 * don't bother with SFU junk here -- just mark inode as needing
528 * revalidation.
529 */
530 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false);
531 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
532 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
533 cifs_fattr_to_inode(inode, &fattr);
534cgfi_exit:
535 FreeXid(xid);
536 return rc;
537}
538
434int cifs_get_inode_info(struct inode **pinode, 539int cifs_get_inode_info(struct inode **pinode,
435 const unsigned char *full_path, FILE_ALL_INFO *pfindData, 540 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
436 struct super_block *sb, int xid, const __u16 *pfid) 541 struct super_block *sb, int xid, const __u16 *pfid)
@@ -610,6 +715,16 @@ cifs_find_inode(struct inode *inode, void *opaque)
610 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid) 715 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
611 return 0; 716 return 0;
612 717
718 /*
719 * uh oh -- it's a directory. We can't use it since hardlinked dirs are
720 * verboten. Disable serverino and return it as if it were found, the
721 * caller can discard it, generate a uniqueid and retry the find
722 */
723 if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
724 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
725 cifs_autodisable_serverino(CIFS_SB(inode->i_sb));
726 }
727
613 return 1; 728 return 1;
614} 729}
615 730
@@ -629,15 +744,22 @@ cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
629 unsigned long hash; 744 unsigned long hash;
630 struct inode *inode; 745 struct inode *inode;
631 746
747retry_iget5_locked:
632 cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid)); 748 cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid));
633 749
634 /* hash down to 32-bits on 32-bit arch */ 750 /* hash down to 32-bits on 32-bit arch */
635 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); 751 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
636 752
637 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr); 753 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
638
639 /* we have fattrs in hand, update the inode */
640 if (inode) { 754 if (inode) {
755 /* was there a problematic inode number collision? */
756 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
757 iput(inode);
758 fattr->cf_uniqueid = iunique(sb, ROOT_I);
759 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
760 goto retry_iget5_locked;
761 }
762
641 cifs_fattr_to_inode(inode, fattr); 763 cifs_fattr_to_inode(inode, fattr);
642 if (sb->s_flags & MS_NOATIME) 764 if (sb->s_flags & MS_NOATIME)
643 inode->i_flags |= S_NOATIME | S_NOCMTIME; 765 inode->i_flags |= S_NOATIME | S_NOCMTIME;
@@ -914,8 +1036,8 @@ undo_setattr:
914/* 1036/*
915 * If dentry->d_inode is null (usually meaning the cached dentry 1037 * If dentry->d_inode is null (usually meaning the cached dentry
916 * is a negative dentry) then we would attempt a standard SMB delete, but 1038 * is a negative dentry) then we would attempt a standard SMB delete, but
917 * if that fails we can not attempt the fall back mechanisms on EACESS 1039 * if that fails we can not attempt the fall back mechanisms on EACCESS
918 * but will return the EACESS to the caller. Note that the VFS does not call 1040 * but will return the EACCESS to the caller. Note that the VFS does not call
919 * unlink on negative dentries currently. 1041 * unlink on negative dentries currently.
920 */ 1042 */
921int cifs_unlink(struct inode *dir, struct dentry *dentry) 1043int cifs_unlink(struct inode *dir, struct dentry *dentry)
@@ -1388,135 +1510,103 @@ cifs_rename_exit:
1388 return rc; 1510 return rc;
1389} 1511}
1390 1512
1391int cifs_revalidate(struct dentry *direntry) 1513static bool
1514cifs_inode_needs_reval(struct inode *inode)
1392{ 1515{
1393 int xid; 1516 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1394 int rc = 0, wbrc = 0;
1395 char *full_path;
1396 struct cifs_sb_info *cifs_sb;
1397 struct cifsInodeInfo *cifsInode;
1398 loff_t local_size;
1399 struct timespec local_mtime;
1400 bool invalidate_inode = false;
1401 1517
1402 if (direntry->d_inode == NULL) 1518 if (cifs_i->clientCanCacheRead)
1403 return -ENOENT; 1519 return false;
1404 1520
1405 cifsInode = CIFS_I(direntry->d_inode); 1521 if (!lookupCacheEnabled)
1522 return true;
1406 1523
1407 if (cifsInode == NULL) 1524 if (cifs_i->time == 0)
1408 return -ENOENT; 1525 return true;
1409 1526
1410 /* no sense revalidating inode info on file that no one can write */ 1527 /* FIXME: the actimeo should be tunable */
1411 if (CIFS_I(direntry->d_inode)->clientCanCacheRead) 1528 if (time_after_eq(jiffies, cifs_i->time + HZ))
1412 return rc; 1529 return true;
1530
1531 return false;
1532}
1533
1534/* check invalid_mapping flag and zap the cache if it's set */
1535static void
1536cifs_invalidate_mapping(struct inode *inode)
1537{
1538 int rc;
1539 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1540
1541 cifs_i->invalid_mapping = false;
1542
1543 /* write back any cached data */
1544 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1545 rc = filemap_write_and_wait(inode->i_mapping);
1546 if (rc)
1547 cifs_i->write_behind_rc = rc;
1548 }
1549 invalidate_remote_inode(inode);
1550}
1551
1552int cifs_revalidate_file(struct file *filp)
1553{
1554 int rc = 0;
1555 struct inode *inode = filp->f_path.dentry->d_inode;
1556
1557 if (!cifs_inode_needs_reval(inode))
1558 goto check_inval;
1559
1560 if (CIFS_SB(inode->i_sb)->tcon->unix_ext)
1561 rc = cifs_get_file_info_unix(filp);
1562 else
1563 rc = cifs_get_file_info(filp);
1564
1565check_inval:
1566 if (CIFS_I(inode)->invalid_mapping)
1567 cifs_invalidate_mapping(inode);
1568
1569 return rc;
1570}
1571
1572/* revalidate a dentry's inode attributes */
1573int cifs_revalidate_dentry(struct dentry *dentry)
1574{
1575 int xid;
1576 int rc = 0;
1577 char *full_path = NULL;
1578 struct inode *inode = dentry->d_inode;
1579 struct super_block *sb = dentry->d_sb;
1580
1581 if (inode == NULL)
1582 return -ENOENT;
1413 1583
1414 xid = GetXid(); 1584 xid = GetXid();
1415 1585
1416 cifs_sb = CIFS_SB(direntry->d_sb); 1586 if (!cifs_inode_needs_reval(inode))
1587 goto check_inval;
1417 1588
1418 /* can not safely grab the rename sem here if rename calls revalidate 1589 /* can not safely grab the rename sem here if rename calls revalidate
1419 since that would deadlock */ 1590 since that would deadlock */
1420 full_path = build_path_from_dentry(direntry); 1591 full_path = build_path_from_dentry(dentry);
1421 if (full_path == NULL) { 1592 if (full_path == NULL) {
1422 rc = -ENOMEM; 1593 rc = -ENOMEM;
1423 FreeXid(xid); 1594 goto check_inval;
1424 return rc;
1425 }
1426 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1427 "jiffies %ld", full_path, direntry->d_inode,
1428 direntry->d_inode->i_count.counter, direntry,
1429 direntry->d_time, jiffies));
1430
1431 if (cifsInode->time == 0) {
1432 /* was set to zero previously to force revalidate */
1433 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1434 lookupCacheEnabled) {
1435 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1436 (direntry->d_inode->i_nlink == 1)) {
1437 kfree(full_path);
1438 FreeXid(xid);
1439 return rc;
1440 } else {
1441 cFYI(1, ("Have to revalidate file due to hardlinks"));
1442 }
1443 }
1444
1445 /* save mtime and size */
1446 local_mtime = direntry->d_inode->i_mtime;
1447 local_size = direntry->d_inode->i_size;
1448
1449 if (cifs_sb->tcon->unix_ext) {
1450 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1451 direntry->d_sb, xid);
1452 if (rc) {
1453 cFYI(1, ("error on getting revalidate info %d", rc));
1454/* if (rc != -ENOENT)
1455 rc = 0; */ /* BB should we cache info on
1456 certain errors? */
1457 }
1458 } else {
1459 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1460 direntry->d_sb, xid, NULL);
1461 if (rc) {
1462 cFYI(1, ("error on getting revalidate info %d", rc));
1463/* if (rc != -ENOENT)
1464 rc = 0; */ /* BB should we cache info on
1465 certain errors? */
1466 }
1467 } 1595 }
1468 /* should we remap certain errors, access denied?, to zero */
1469 1596
1470 /* if not oplocked, we invalidate inode pages if mtime or file size 1597 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1471 had changed on server */ 1598 "jiffies %ld", full_path, inode, inode->i_count.counter,
1599 dentry, dentry->d_time, jiffies));
1472 1600
1473 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && 1601 if (CIFS_SB(sb)->tcon->unix_ext)
1474 (local_size == direntry->d_inode->i_size)) { 1602 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1475 cFYI(1, ("cifs_revalidate - inode unchanged")); 1603 else
1476 } else { 1604 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1477 /* file may have changed on server */ 1605 xid, NULL);
1478 if (cifsInode->clientCanCacheRead) {
1479 /* no need to invalidate inode pages since we were the
1480 only ones who could have modified the file and the
1481 server copy is staler than ours */
1482 } else {
1483 invalidate_inode = true;
1484 }
1485 }
1486 1606
1487 /* can not grab this sem since kernel filesys locking documentation 1607check_inval:
1488 indicates i_mutex may be taken by the kernel on lookup and rename 1608 if (CIFS_I(inode)->invalid_mapping)
1489 which could deadlock if we grab the i_mutex here as well */ 1609 cifs_invalidate_mapping(inode);
1490/* mutex_lock(&direntry->d_inode->i_mutex);*/
1491 /* need to write out dirty pages here */
1492 if (direntry->d_inode->i_mapping) {
1493 /* do we need to lock inode until after invalidate completes
1494 below? */
1495 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1496 if (wbrc)
1497 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1498 }
1499 if (invalidate_inode) {
1500 /* shrink_dcache not necessary now that cifs dentry ops
1501 are exported for negative dentries */
1502/* if (S_ISDIR(direntry->d_inode->i_mode))
1503 shrink_dcache_parent(direntry); */
1504 if (S_ISREG(direntry->d_inode->i_mode)) {
1505 if (direntry->d_inode->i_mapping) {
1506 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1507 if (wbrc)
1508 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1509 }
1510 /* may eventually have to do this for open files too */
1511 if (list_empty(&(cifsInode->openFileList))) {
1512 /* changed on server - flush read ahead pages */
1513 cFYI(1, ("Invalidating read ahead data on "
1514 "closed file"));
1515 invalidate_remote_inode(direntry->d_inode);
1516 }
1517 }
1518 }
1519/* mutex_unlock(&direntry->d_inode->i_mutex); */
1520 1610
1521 kfree(full_path); 1611 kfree(full_path);
1522 FreeXid(xid); 1612 FreeXid(xid);
@@ -1526,7 +1616,7 @@ int cifs_revalidate(struct dentry *direntry)
1526int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, 1616int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1527 struct kstat *stat) 1617 struct kstat *stat)
1528{ 1618{
1529 int err = cifs_revalidate(dentry); 1619 int err = cifs_revalidate_dentry(dentry);
1530 if (!err) { 1620 if (!err) {
1531 generic_fillattr(dentry->d_inode, stat); 1621 generic_fillattr(dentry->d_inode, stat);
1532 stat->blksize = CIFS_MAX_MSGSIZE; 1622 stat->blksize = CIFS_MAX_MSGSIZE;
@@ -1762,8 +1852,18 @@ cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
1762 CIFS_MOUNT_MAP_SPECIAL_CHR); 1852 CIFS_MOUNT_MAP_SPECIAL_CHR);
1763 } 1853 }
1764 1854
1765 if (!rc) 1855 if (!rc) {
1766 rc = inode_setattr(inode, attrs); 1856 rc = inode_setattr(inode, attrs);
1857
1858 /* force revalidate when any of these times are set since some
1859 of the fs types (eg ext3, fat) do not have fine enough
1860 time granularity to match protocol, and we do not have a
1861 a way (yet) to query the server fs's time granularity (and
1862 whether it rounds times down).
1863 */
1864 if (!rc && (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME)))
1865 cifsInode->time = 0;
1866 }
1767out: 1867out:
1768 kfree(args); 1868 kfree(args);
1769 kfree(full_path); 1869 kfree(full_path);