Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/inode.c |
| 3 | * |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Lesser General Public License as published |
| 9 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/stat.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <asm/div64.h> |
| 25 | #include "cifsfs.h" |
| 26 | #include "cifspdu.h" |
| 27 | #include "cifsglob.h" |
| 28 | #include "cifsproto.h" |
| 29 | #include "cifs_debug.h" |
| 30 | #include "cifs_fs_sb.h" |
| 31 | |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 32 | |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 33 | static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral) |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 34 | { |
| 35 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 36 | |
| 37 | switch (inode->i_mode & S_IFMT) { |
| 38 | case S_IFREG: |
| 39 | inode->i_op = &cifs_file_inode_ops; |
| 40 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
| 41 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 42 | inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 43 | else |
| 44 | inode->i_fop = &cifs_file_direct_ops; |
| 45 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 46 | inode->i_fop = &cifs_file_nobrl_ops; |
| 47 | else { /* not direct, send byte range locks */ |
| 48 | inode->i_fop = &cifs_file_ops; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /* check if server can support readpages */ |
| 53 | if (cifs_sb->tcon->ses->server->maxBuf < |
| 54 | PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE) |
| 55 | inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 56 | else |
| 57 | inode->i_data.a_ops = &cifs_addr_ops; |
| 58 | break; |
| 59 | case S_IFDIR: |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 61 | if (is_dfs_referral) { |
| 62 | inode->i_op = &cifs_dfs_referral_inode_operations; |
| 63 | } else { |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 64 | #else /* NO DFS support, treat as a directory */ |
| 65 | { |
| 66 | #endif |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 67 | inode->i_op = &cifs_dir_inode_ops; |
| 68 | inode->i_fop = &cifs_dir_ops; |
| 69 | } |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 70 | break; |
| 71 | case S_IFLNK: |
| 72 | inode->i_op = &cifs_symlink_inode_ops; |
| 73 | break; |
| 74 | default: |
| 75 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 80 | /* populate an inode with info from a cifs_fattr struct */ |
| 81 | void |
| 82 | cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 83 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 84 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 85 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 86 | unsigned long oldtime = cifs_i->time; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 87 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 88 | inode->i_atime = fattr->cf_atime; |
| 89 | inode->i_mtime = fattr->cf_mtime; |
| 90 | inode->i_ctime = fattr->cf_ctime; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 91 | inode->i_rdev = fattr->cf_rdev; |
| 92 | inode->i_nlink = fattr->cf_nlink; |
| 93 | inode->i_uid = fattr->cf_uid; |
| 94 | inode->i_gid = fattr->cf_gid; |
| 95 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 96 | /* if dynperm is set, don't clobber existing mode */ |
| 97 | if (inode->i_state & I_NEW || |
| 98 | !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) |
| 99 | inode->i_mode = fattr->cf_mode; |
| 100 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 101 | cifs_i->cifsAttrs = fattr->cf_cifsattrs; |
| 102 | cifs_i->uniqueid = fattr->cf_uniqueid; |
| 103 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 104 | if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL) |
| 105 | cifs_i->time = 0; |
| 106 | else |
| 107 | cifs_i->time = jiffies; |
| 108 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 109 | cFYI(1, ("inode 0x%p old_time=%ld new_time=%ld", inode, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 110 | oldtime, cifs_i->time)); |
| 111 | |
| 112 | cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 113 | |
Jeff Layton | 835a36c | 2010-02-10 16:21:33 -0500 | [diff] [blame^] | 114 | cifs_i->server_eof = fattr->cf_eof; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 115 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 116 | * Can't safely change the file size here if the client is writing to |
| 117 | * it due to potential races. |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 118 | */ |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 119 | spin_lock(&inode->i_lock); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 120 | if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) { |
| 121 | i_size_write(inode, fattr->cf_eof); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * i_blocks is not related to (i_size / i_blksize), |
| 125 | * but instead 512 byte (2**9) size is required for |
| 126 | * calculating num blocks. |
| 127 | */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 128 | inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 129 | } |
| 130 | spin_unlock(&inode->i_lock); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 131 | |
| 132 | cifs_set_ops(inode, fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 135 | /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */ |
| 136 | void |
| 137 | cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info, |
| 138 | struct cifs_sb_info *cifs_sb) |
| 139 | { |
| 140 | memset(fattr, 0, sizeof(*fattr)); |
| 141 | fattr->cf_uniqueid = le64_to_cpu(info->UniqueId); |
| 142 | fattr->cf_bytes = le64_to_cpu(info->NumOfBytes); |
| 143 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 144 | |
| 145 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 146 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime); |
| 147 | fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange); |
| 148 | fattr->cf_mode = le64_to_cpu(info->Permissions); |
| 149 | |
| 150 | /* |
| 151 | * Since we set the inode type below we need to mask off |
| 152 | * to avoid strange results if bits set above. |
| 153 | */ |
| 154 | fattr->cf_mode &= ~S_IFMT; |
| 155 | switch (le32_to_cpu(info->Type)) { |
| 156 | case UNIX_FILE: |
| 157 | fattr->cf_mode |= S_IFREG; |
| 158 | fattr->cf_dtype = DT_REG; |
| 159 | break; |
| 160 | case UNIX_SYMLINK: |
| 161 | fattr->cf_mode |= S_IFLNK; |
| 162 | fattr->cf_dtype = DT_LNK; |
| 163 | break; |
| 164 | case UNIX_DIR: |
| 165 | fattr->cf_mode |= S_IFDIR; |
| 166 | fattr->cf_dtype = DT_DIR; |
| 167 | break; |
| 168 | case UNIX_CHARDEV: |
| 169 | fattr->cf_mode |= S_IFCHR; |
| 170 | fattr->cf_dtype = DT_CHR; |
| 171 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 172 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 173 | break; |
| 174 | case UNIX_BLOCKDEV: |
| 175 | fattr->cf_mode |= S_IFBLK; |
| 176 | fattr->cf_dtype = DT_BLK; |
| 177 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 178 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 179 | break; |
| 180 | case UNIX_FIFO: |
| 181 | fattr->cf_mode |= S_IFIFO; |
| 182 | fattr->cf_dtype = DT_FIFO; |
| 183 | break; |
| 184 | case UNIX_SOCKET: |
| 185 | fattr->cf_mode |= S_IFSOCK; |
| 186 | fattr->cf_dtype = DT_SOCK; |
| 187 | break; |
| 188 | default: |
| 189 | /* safest to call it a file if we do not know */ |
| 190 | fattr->cf_mode |= S_IFREG; |
| 191 | fattr->cf_dtype = DT_REG; |
| 192 | cFYI(1, ("unknown type %d", le32_to_cpu(info->Type))); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) |
| 197 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 198 | else |
| 199 | fattr->cf_uid = le64_to_cpu(info->Uid); |
| 200 | |
| 201 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) |
| 202 | fattr->cf_gid = cifs_sb->mnt_gid; |
| 203 | else |
| 204 | fattr->cf_gid = le64_to_cpu(info->Gid); |
| 205 | |
| 206 | fattr->cf_nlink = le64_to_cpu(info->Nlinks); |
| 207 | } |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 210 | * Fill a cifs_fattr struct with fake inode info. |
| 211 | * |
| 212 | * Needed to setup cifs_fattr data for the directory which is the |
| 213 | * junction to the new submount (ie to setup the fake directory |
| 214 | * which represents a DFS referral). |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 215 | */ |
Steve French | f1230c9 | 2009-07-22 23:13:01 +0000 | [diff] [blame] | 216 | static void |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 217 | cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb) |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 218 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 219 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 220 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 221 | cFYI(1, ("creating fake fattr for DFS referral")); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 222 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 223 | memset(fattr, 0, sizeof(*fattr)); |
| 224 | fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU; |
| 225 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 226 | fattr->cf_gid = cifs_sb->mnt_gid; |
| 227 | fattr->cf_atime = CURRENT_TIME; |
| 228 | fattr->cf_ctime = CURRENT_TIME; |
| 229 | fattr->cf_mtime = CURRENT_TIME; |
| 230 | fattr->cf_nlink = 2; |
| 231 | fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | int cifs_get_inode_info_unix(struct inode **pinode, |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 235 | const unsigned char *full_path, |
| 236 | struct super_block *sb, int xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 238 | int rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 239 | FILE_UNIX_BASIC_INFO find_data; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 240 | struct cifs_fattr fattr; |
| 241 | struct cifsTconInfo *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 244 | tcon = cifs_sb->tcon; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 245 | cFYI(1, ("Getting info on %s", full_path)); |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* could have done a find first instead but this returns more info */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 248 | rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 249 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 250 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 251 | |
| 252 | if (!rc) { |
| 253 | cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb); |
| 254 | } else if (rc == -EREMOTE) { |
| 255 | cifs_create_dfs_fattr(&fattr, sb); |
Jeff Layton | e911d0c | 2008-07-12 13:47:59 -0700 | [diff] [blame] | 256 | rc = 0; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 257 | } else { |
| 258 | return rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 261 | if (*pinode == NULL) { |
| 262 | /* get new inode */ |
| 263 | *pinode = cifs_iget(sb, &fattr); |
| 264 | if (!*pinode) |
| 265 | rc = -ENOMEM; |
| 266 | } else { |
| 267 | /* we already have inode, update it */ |
| 268 | cifs_fattr_to_inode(*pinode, &fattr); |
| 269 | } |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return rc; |
| 272 | } |
| 273 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 274 | static int |
| 275 | cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path, |
| 276 | struct cifs_sb_info *cifs_sb, int xid) |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 277 | { |
| 278 | int rc; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 279 | int oplock = 0; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 280 | __u16 netfid; |
| 281 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 282 | char buf[24]; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 283 | unsigned int bytes_read; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 284 | char *pbuf; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 285 | |
| 286 | pbuf = buf; |
| 287 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 288 | fattr->cf_mode &= ~S_IFMT; |
| 289 | |
| 290 | if (fattr->cf_eof == 0) { |
| 291 | fattr->cf_mode |= S_IFIFO; |
| 292 | fattr->cf_dtype = DT_FIFO; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 293 | return 0; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 294 | } else if (fattr->cf_eof < 8) { |
| 295 | fattr->cf_mode |= S_IFREG; |
| 296 | fattr->cf_dtype = DT_REG; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 297 | return -EINVAL; /* EOPNOTSUPP? */ |
| 298 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 299 | |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 300 | rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ, |
| 301 | CREATE_NOT_DIR, &netfid, &oplock, NULL, |
| 302 | cifs_sb->local_nls, |
| 303 | cifs_sb->mnt_cifs_flags & |
| 304 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 305 | if (rc == 0) { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 306 | int buf_type = CIFS_NO_BUFFER; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 307 | /* Read header */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 308 | rc = CIFSSMBRead(xid, pTcon, netfid, |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 309 | 24 /* length */, 0 /* offset */, |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 310 | &bytes_read, &pbuf, &buf_type); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 311 | if ((rc == 0) && (bytes_read >= 8)) { |
| 312 | if (memcmp("IntxBLK", pbuf, 8) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 313 | cFYI(1, ("Block device")); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 314 | fattr->cf_mode |= S_IFBLK; |
| 315 | fattr->cf_dtype = DT_BLK; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 316 | if (bytes_read == 24) { |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 317 | /* we have enough to decode dev num */ |
| 318 | __u64 mjr; /* major */ |
| 319 | __u64 mnr; /* minor */ |
| 320 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 321 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 322 | fattr->cf_rdev = MKDEV(mjr, mnr); |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 323 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 324 | } else if (memcmp("IntxCHR", pbuf, 8) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 325 | cFYI(1, ("Char device")); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 326 | fattr->cf_mode |= S_IFCHR; |
| 327 | fattr->cf_dtype = DT_CHR; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 328 | if (bytes_read == 24) { |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 329 | /* we have enough to decode dev num */ |
| 330 | __u64 mjr; /* major */ |
| 331 | __u64 mnr; /* minor */ |
| 332 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 333 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 334 | fattr->cf_rdev = MKDEV(mjr, mnr); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 335 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 336 | } else if (memcmp("IntxLNK", pbuf, 7) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 337 | cFYI(1, ("Symlink")); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 338 | fattr->cf_mode |= S_IFLNK; |
| 339 | fattr->cf_dtype = DT_LNK; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 340 | } else { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 341 | fattr->cf_mode |= S_IFREG; /* file? */ |
| 342 | fattr->cf_dtype = DT_REG; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 343 | rc = -EOPNOTSUPP; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 344 | } |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 345 | } else { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 346 | fattr->cf_mode |= S_IFREG; /* then it is a file */ |
| 347 | fattr->cf_dtype = DT_REG; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 348 | rc = -EOPNOTSUPP; /* or some unknown SFU type */ |
| 349 | } |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 350 | CIFSSMBClose(xid, pTcon, netfid); |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 351 | } |
| 352 | return rc; |
Steve French | d6e2f2a | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 355 | #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ |
| 356 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 357 | /* |
| 358 | * Fetch mode bits as provided by SFU. |
| 359 | * |
| 360 | * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ? |
| 361 | */ |
| 362 | static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path, |
| 363 | struct cifs_sb_info *cifs_sb, int xid) |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 364 | { |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 365 | #ifdef CONFIG_CIFS_XATTR |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 366 | ssize_t rc; |
| 367 | char ea_value[4]; |
| 368 | __u32 mode; |
| 369 | |
Jeff Layton | 31c0519 | 2010-02-10 16:18:26 -0500 | [diff] [blame] | 370 | rc = CIFSSMBQAllEAs(xid, cifs_sb->tcon, path, "SETFILEBITS", |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 371 | ea_value, 4 /* size of buf */, cifs_sb->local_nls, |
| 372 | cifs_sb->mnt_cifs_flags & |
| 373 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 374 | if (rc < 0) |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 375 | return (int)rc; |
| 376 | else if (rc > 3) { |
| 377 | mode = le32_to_cpu(*((__le32 *)ea_value)); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 378 | fattr->cf_mode &= ~SFBITS_MASK; |
| 379 | cFYI(1, ("special bits 0%o org mode 0%o", mode, |
| 380 | fattr->cf_mode)); |
| 381 | fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 382 | cFYI(1, ("special mode bits 0%o", mode)); |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 383 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 384 | |
| 385 | return 0; |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 386 | #else |
| 387 | return -EOPNOTSUPP; |
| 388 | #endif |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 391 | /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */ |
Steve French | f1230c9 | 2009-07-22 23:13:01 +0000 | [diff] [blame] | 392 | static void |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 393 | cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info, |
| 394 | struct cifs_sb_info *cifs_sb, bool adjust_tz) |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 395 | { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 396 | memset(fattr, 0, sizeof(*fattr)); |
| 397 | fattr->cf_cifsattrs = le32_to_cpu(info->Attributes); |
| 398 | if (info->DeletePending) |
| 399 | fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING; |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 400 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 401 | if (info->LastAccessTime) |
| 402 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 403 | else |
| 404 | fattr->cf_atime = CURRENT_TIME; |
| 405 | |
| 406 | fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime); |
| 407 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); |
| 408 | |
| 409 | if (adjust_tz) { |
| 410 | fattr->cf_ctime.tv_sec += cifs_sb->tcon->ses->server->timeAdj; |
| 411 | fattr->cf_mtime.tv_sec += cifs_sb->tcon->ses->server->timeAdj; |
| 412 | } |
| 413 | |
| 414 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 415 | fattr->cf_bytes = le64_to_cpu(info->AllocationSize); |
| 416 | |
| 417 | if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { |
| 418 | fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode; |
| 419 | fattr->cf_dtype = DT_DIR; |
| 420 | } else { |
| 421 | fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode; |
| 422 | fattr->cf_dtype = DT_REG; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 423 | |
Jeff Layton | d0c280d | 2009-07-09 01:46:44 -0400 | [diff] [blame] | 424 | /* clear write bits if ATTR_READONLY is set */ |
| 425 | if (fattr->cf_cifsattrs & ATTR_READONLY) |
| 426 | fattr->cf_mode &= ~(S_IWUGO); |
| 427 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 428 | |
| 429 | fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks); |
| 430 | |
| 431 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 432 | fattr->cf_gid = cifs_sb->mnt_gid; |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | int cifs_get_inode_info(struct inode **pinode, |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 436 | const unsigned char *full_path, FILE_ALL_INFO *pfindData, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 437 | struct super_block *sb, int xid, const __u16 *pfid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 439 | int rc = 0, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | struct cifsTconInfo *pTcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | char *buf = NULL; |
Steve French | 5ade9de | 2008-05-02 20:56:23 +0000 | [diff] [blame] | 443 | bool adjustTZ = false; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 444 | struct cifs_fattr fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | pTcon = cifs_sb->tcon; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 447 | cFYI(1, ("Getting info on %s", full_path)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 449 | if ((pfindData == NULL) && (*pinode != NULL)) { |
| 450 | if (CIFS_I(*pinode)->clientCanCacheRead) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 451 | cFYI(1, ("No need to revalidate cached inode sizes")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return rc; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* if file info not passed in then get it from server */ |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 457 | if (pfindData == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 459 | if (buf == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | return -ENOMEM; |
| 461 | pfindData = (FILE_ALL_INFO *)buf; |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* could do find first instead but this returns more info */ |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 464 | rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData, |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 465 | 0 /* not legacy */, |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 466 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 467 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 468 | /* BB optimize code so we do not make the above call |
| 469 | when server claims no NT SMB support and the above call |
| 470 | failed at least once - set flag in tcon or mount */ |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 471 | if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 472 | rc = SMBQueryInformation(xid, pTcon, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 473 | pfindData, cifs_sb->local_nls, |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 474 | cifs_sb->mnt_cifs_flags & |
| 475 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 476 | adjustTZ = true; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 479 | |
| 480 | if (!rc) { |
| 481 | cifs_all_info_to_fattr(&fattr, (FILE_ALL_INFO *) pfindData, |
| 482 | cifs_sb, adjustTZ); |
| 483 | } else if (rc == -EREMOTE) { |
| 484 | cifs_create_dfs_fattr(&fattr, sb); |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 485 | rc = 0; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 486 | } else { |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 487 | goto cgii_exit; |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 490 | /* |
| 491 | * If an inode wasn't passed in, then get the inode number |
| 492 | * |
| 493 | * Is an i_ino of zero legal? Can we use that to check if the server |
| 494 | * supports returning inode numbers? Are there other sanity checks we |
| 495 | * can use to ensure that the server is really filling in that field? |
| 496 | * |
| 497 | * We can not use the IndexNumber field by default from Windows or |
| 498 | * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA |
| 499 | * CIFS spec claims that this value is unique within the scope of a |
| 500 | * share, and the windows docs hint that it's actually unique |
| 501 | * per-machine. |
| 502 | * |
| 503 | * There may be higher info levels that work but are there Windows |
| 504 | * server or network appliances for which IndexNumber field is not |
| 505 | * guaranteed unique? |
| 506 | */ |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 507 | if (*pinode == NULL) { |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 508 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
| 509 | int rc1 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 511 | rc1 = CIFSGetSrvInodeNumber(xid, pTcon, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 512 | full_path, &fattr.cf_uniqueid, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 513 | cifs_sb->local_nls, |
| 514 | cifs_sb->mnt_cifs_flags & |
| 515 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | ec06aed | 2009-11-06 14:18:29 -0500 | [diff] [blame] | 516 | if (rc1 || !fattr.cf_uniqueid) { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 517 | cFYI(1, ("GetSrvInodeNum rc %d", rc1)); |
| 518 | fattr.cf_uniqueid = iunique(sb, ROOT_I); |
Jeff Layton | ec06aed | 2009-11-06 14:18:29 -0500 | [diff] [blame] | 519 | cifs_autodisable_serverino(cifs_sb); |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 520 | } |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 521 | } else { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 522 | fattr.cf_uniqueid = iunique(sb, ROOT_I); |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 523 | } |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 524 | } else { |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 525 | fattr.cf_uniqueid = CIFS_I(*pinode)->uniqueid; |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 528 | /* query for SFU type info if supported and needed */ |
| 529 | if (fattr.cf_cifsattrs & ATTR_SYSTEM && |
| 530 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
| 531 | tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid); |
| 532 | if (tmprc) |
| 533 | cFYI(1, ("cifs_sfu_type failed: %d", tmprc)); |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 534 | } |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 535 | |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 536 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 537 | /* fill in 0777 bits from ACL */ |
| 538 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
| 539 | cFYI(1, ("Getting mode bits from ACL")); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 540 | cifs_acl_to_fattr(cifs_sb, &fattr, *pinode, full_path, pfid); |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 541 | } |
| 542 | #endif |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 543 | |
| 544 | /* fill in remaining high mode bits e.g. SUID, VTX */ |
| 545 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) |
| 546 | cifs_sfu_mode(&fattr, full_path, cifs_sb, xid); |
| 547 | |
| 548 | if (!*pinode) { |
| 549 | *pinode = cifs_iget(sb, &fattr); |
| 550 | if (!*pinode) |
| 551 | rc = -ENOMEM; |
| 552 | } else { |
| 553 | cifs_fattr_to_inode(*pinode, &fattr); |
Steve French | b9a3260 | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 556 | cgii_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | kfree(buf); |
| 558 | return rc; |
| 559 | } |
| 560 | |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 561 | static const struct inode_operations cifs_ipc_inode_ops = { |
| 562 | .lookup = cifs_lookup, |
| 563 | }; |
| 564 | |
Igor Mammedov | e4cce94 | 2009-02-10 14:10:26 +0300 | [diff] [blame] | 565 | char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb) |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 566 | { |
| 567 | int pplen = cifs_sb->prepathlen; |
| 568 | int dfsplen; |
| 569 | char *full_path = NULL; |
| 570 | |
| 571 | /* if no prefix path, simply set path to the root of share to "" */ |
| 572 | if (pplen == 0) { |
| 573 | full_path = kmalloc(1, GFP_KERNEL); |
| 574 | if (full_path) |
| 575 | full_path[0] = 0; |
| 576 | return full_path; |
| 577 | } |
| 578 | |
| 579 | if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS)) |
| 580 | dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1); |
| 581 | else |
| 582 | dfsplen = 0; |
| 583 | |
| 584 | full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); |
| 585 | if (full_path == NULL) |
| 586 | return full_path; |
| 587 | |
| 588 | if (dfsplen) { |
| 589 | strncpy(full_path, cifs_sb->tcon->treeName, dfsplen); |
| 590 | /* switch slash direction in prepath depending on whether |
| 591 | * windows or posix style path names |
| 592 | */ |
| 593 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
| 594 | int i; |
| 595 | for (i = 0; i < dfsplen; i++) { |
| 596 | if (full_path[i] == '\\') |
| 597 | full_path[i] = '/'; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | strncpy(full_path + dfsplen, cifs_sb->prepath, pplen); |
| 602 | full_path[dfsplen + pplen] = 0; /* add trailing null */ |
| 603 | return full_path; |
| 604 | } |
| 605 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 606 | static int |
| 607 | cifs_find_inode(struct inode *inode, void *opaque) |
| 608 | { |
| 609 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 610 | |
| 611 | if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid) |
| 612 | return 0; |
| 613 | |
| 614 | return 1; |
| 615 | } |
| 616 | |
| 617 | static int |
| 618 | cifs_init_inode(struct inode *inode, void *opaque) |
| 619 | { |
| 620 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 621 | |
| 622 | CIFS_I(inode)->uniqueid = fattr->cf_uniqueid; |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* Given fattrs, get a corresponding inode */ |
| 627 | struct inode * |
| 628 | cifs_iget(struct super_block *sb, struct cifs_fattr *fattr) |
| 629 | { |
| 630 | unsigned long hash; |
| 631 | struct inode *inode; |
| 632 | |
| 633 | cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid)); |
| 634 | |
| 635 | /* hash down to 32-bits on 32-bit arch */ |
| 636 | hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); |
| 637 | |
| 638 | inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr); |
| 639 | |
| 640 | /* we have fattrs in hand, update the inode */ |
| 641 | if (inode) { |
| 642 | cifs_fattr_to_inode(inode, fattr); |
| 643 | if (sb->s_flags & MS_NOATIME) |
| 644 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 645 | if (inode->i_state & I_NEW) { |
| 646 | inode->i_ino = hash; |
| 647 | unlock_new_inode(inode); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return inode; |
| 652 | } |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* gets root inode */ |
Jeff Layton | bd433d4 | 2009-05-27 09:37:34 -0400 | [diff] [blame] | 655 | struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 657 | int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 659 | struct inode *inode = NULL; |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 660 | long rc; |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 661 | char *full_path; |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 662 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 663 | cifs_sb = CIFS_SB(sb); |
Igor Mammedov | e4cce94 | 2009-02-10 14:10:26 +0300 | [diff] [blame] | 664 | full_path = cifs_build_path_to_root(cifs_sb); |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 665 | if (full_path == NULL) |
| 666 | return ERR_PTR(-ENOMEM); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 667 | |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 668 | xid = GetXid(); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 669 | if (cifs_sb->tcon->unix_ext) |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 670 | rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 671 | else |
| 672 | rc = cifs_get_inode_info(&inode, full_path, NULL, sb, |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 673 | xid, NULL); |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 674 | |
| 675 | if (!inode) |
| 676 | return ERR_PTR(-ENOMEM); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 677 | |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 678 | if (rc && cifs_sb->tcon->ipc) { |
| 679 | cFYI(1, ("ipc connection - fake read inode")); |
| 680 | inode->i_mode |= S_IFDIR; |
| 681 | inode->i_nlink = 2; |
| 682 | inode->i_op = &cifs_ipc_inode_ops; |
| 683 | inode->i_fop = &simple_dir_operations; |
| 684 | inode->i_uid = cifs_sb->mnt_uid; |
| 685 | inode->i_gid = cifs_sb->mnt_gid; |
Steve French | ad66133 | 2008-08-12 14:14:40 +0000 | [diff] [blame] | 686 | } else if (rc) { |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 687 | kfree(full_path); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 688 | _FreeXid(xid); |
| 689 | iget_failed(inode); |
| 690 | return ERR_PTR(rc); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 691 | } |
| 692 | |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 693 | |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 694 | kfree(full_path); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 695 | /* can not call macro FreeXid here since in a void func |
| 696 | * TODO: This is no longer true |
| 697 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | _FreeXid(xid); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 699 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 702 | static int |
| 703 | cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid, |
| 704 | char *full_path, __u32 dosattr) |
| 705 | { |
| 706 | int rc; |
| 707 | int oplock = 0; |
| 708 | __u16 netfid; |
| 709 | __u32 netpid; |
| 710 | bool set_time = false; |
| 711 | struct cifsFileInfo *open_file; |
| 712 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 713 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 714 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 715 | FILE_BASIC_INFO info_buf; |
| 716 | |
Steve French | 1adcb71 | 2009-02-25 14:19:56 +0000 | [diff] [blame] | 717 | if (attrs == NULL) |
| 718 | return -EINVAL; |
| 719 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 720 | if (attrs->ia_valid & ATTR_ATIME) { |
| 721 | set_time = true; |
| 722 | info_buf.LastAccessTime = |
| 723 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); |
| 724 | } else |
| 725 | info_buf.LastAccessTime = 0; |
| 726 | |
| 727 | if (attrs->ia_valid & ATTR_MTIME) { |
| 728 | set_time = true; |
| 729 | info_buf.LastWriteTime = |
| 730 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); |
| 731 | } else |
| 732 | info_buf.LastWriteTime = 0; |
| 733 | |
| 734 | /* |
| 735 | * Samba throws this field away, but windows may actually use it. |
| 736 | * Do not set ctime unless other time stamps are changed explicitly |
| 737 | * (i.e. by utimes()) since we would then have a mix of client and |
| 738 | * server times. |
| 739 | */ |
| 740 | if (set_time && (attrs->ia_valid & ATTR_CTIME)) { |
| 741 | cFYI(1, ("CIFS - CTIME changed")); |
| 742 | info_buf.ChangeTime = |
| 743 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); |
| 744 | } else |
| 745 | info_buf.ChangeTime = 0; |
| 746 | |
| 747 | info_buf.CreationTime = 0; /* don't change */ |
| 748 | info_buf.Attributes = cpu_to_le32(dosattr); |
| 749 | |
| 750 | /* |
| 751 | * If the file is already open for write, just use that fileid |
| 752 | */ |
| 753 | open_file = find_writable_file(cifsInode); |
| 754 | if (open_file) { |
| 755 | netfid = open_file->netfid; |
| 756 | netpid = open_file->pid; |
| 757 | goto set_via_filehandle; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * NT4 apparently returns success on this call, but it doesn't |
| 762 | * really work. |
| 763 | */ |
| 764 | if (!(pTcon->ses->flags & CIFS_SES_NT4)) { |
| 765 | rc = CIFSSMBSetPathInfo(xid, pTcon, full_path, |
| 766 | &info_buf, cifs_sb->local_nls, |
| 767 | cifs_sb->mnt_cifs_flags & |
| 768 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 769 | if (rc == 0) { |
| 770 | cifsInode->cifsAttrs = dosattr; |
| 771 | goto out; |
| 772 | } else if (rc != -EOPNOTSUPP && rc != -EINVAL) |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | cFYI(1, ("calling SetFileInfo since SetPathInfo for " |
| 777 | "times not supported by this server")); |
| 778 | rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, |
| 779 | SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, |
| 780 | CREATE_NOT_DIR, &netfid, &oplock, |
| 781 | NULL, cifs_sb->local_nls, |
| 782 | cifs_sb->mnt_cifs_flags & |
| 783 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 784 | |
| 785 | if (rc != 0) { |
| 786 | if (rc == -EIO) |
| 787 | rc = -EINVAL; |
| 788 | goto out; |
| 789 | } |
| 790 | |
| 791 | netpid = current->tgid; |
| 792 | |
| 793 | set_via_filehandle: |
| 794 | rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid); |
Steve French | d388908 | 2008-09-24 19:22:52 +0000 | [diff] [blame] | 795 | if (!rc) |
| 796 | cifsInode->cifsAttrs = dosattr; |
| 797 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 798 | if (open_file == NULL) |
| 799 | CIFSSMBClose(xid, pTcon, netfid); |
| 800 | else |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 801 | cifsFileInfo_put(open_file); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 802 | out: |
| 803 | return rc; |
| 804 | } |
| 805 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 806 | /* |
| 807 | * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit |
| 808 | * and rename it to a random name that hopefully won't conflict with |
| 809 | * anything else. |
| 810 | */ |
| 811 | static int |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 812 | cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid) |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 813 | { |
| 814 | int oplock = 0; |
| 815 | int rc; |
| 816 | __u16 netfid; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 817 | struct inode *inode = dentry->d_inode; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 818 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 819 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 820 | struct cifsTconInfo *tcon = cifs_sb->tcon; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 821 | __u32 dosattr, origattr; |
| 822 | FILE_BASIC_INFO *info_buf = NULL; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 823 | |
| 824 | rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN, |
Jeff Layton | dd1db2d | 2008-10-16 19:27:12 -0400 | [diff] [blame] | 825 | DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR, |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 826 | &netfid, &oplock, NULL, cifs_sb->local_nls, |
| 827 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 828 | if (rc != 0) |
| 829 | goto out; |
| 830 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 831 | origattr = cifsInode->cifsAttrs; |
| 832 | if (origattr == 0) |
| 833 | origattr |= ATTR_NORMAL; |
| 834 | |
| 835 | dosattr = origattr & ~ATTR_READONLY; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 836 | if (dosattr == 0) |
| 837 | dosattr |= ATTR_NORMAL; |
| 838 | dosattr |= ATTR_HIDDEN; |
| 839 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 840 | /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */ |
| 841 | if (dosattr != origattr) { |
| 842 | info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL); |
| 843 | if (info_buf == NULL) { |
| 844 | rc = -ENOMEM; |
| 845 | goto out_close; |
| 846 | } |
| 847 | info_buf->Attributes = cpu_to_le32(dosattr); |
| 848 | rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid, |
| 849 | current->tgid); |
| 850 | /* although we would like to mark the file hidden |
| 851 | if that fails we will still try to rename it */ |
Steve French | 4134609 | 2008-10-20 18:24:42 +0000 | [diff] [blame] | 852 | if (rc != 0) |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 853 | cifsInode->cifsAttrs = dosattr; |
| 854 | else |
| 855 | dosattr = origattr; /* since not able to change them */ |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 856 | } |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 857 | |
Jeff Layton | dd1db2d | 2008-10-16 19:27:12 -0400 | [diff] [blame] | 858 | /* rename the file */ |
| 859 | rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls, |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 860 | cifs_sb->mnt_cifs_flags & |
| 861 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 862 | if (rc != 0) { |
| 863 | rc = -ETXTBSY; |
| 864 | goto undo_setattr; |
| 865 | } |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 866 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 867 | /* try to set DELETE_ON_CLOSE */ |
| 868 | if (!cifsInode->delete_pending) { |
| 869 | rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid, |
| 870 | current->tgid); |
| 871 | /* |
| 872 | * some samba versions return -ENOENT when we try to set the |
| 873 | * file disposition here. Likely a samba bug, but work around |
| 874 | * it for now. This means that some cifsXXX files may hang |
| 875 | * around after they shouldn't. |
| 876 | * |
| 877 | * BB: remove this hack after more servers have the fix |
| 878 | */ |
| 879 | if (rc == -ENOENT) |
| 880 | rc = 0; |
| 881 | else if (rc != 0) { |
| 882 | rc = -ETXTBSY; |
| 883 | goto undo_rename; |
| 884 | } |
| 885 | cifsInode->delete_pending = true; |
| 886 | } |
Jeff Layton | 7ce86d5 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 887 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 888 | out_close: |
| 889 | CIFSSMBClose(xid, tcon, netfid); |
| 890 | out: |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 891 | kfree(info_buf); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 892 | return rc; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 893 | |
| 894 | /* |
| 895 | * reset everything back to the original state. Don't bother |
| 896 | * dealing with errors here since we can't do anything about |
| 897 | * them anyway. |
| 898 | */ |
| 899 | undo_rename: |
| 900 | CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name, |
| 901 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 902 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 903 | undo_setattr: |
| 904 | if (dosattr != origattr) { |
| 905 | info_buf->Attributes = cpu_to_le32(origattr); |
| 906 | if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid, |
| 907 | current->tgid)) |
| 908 | cifsInode->cifsAttrs = origattr; |
| 909 | } |
| 910 | |
| 911 | goto out_close; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 912 | } |
| 913 | |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * If dentry->d_inode is null (usually meaning the cached dentry |
| 917 | * is a negative dentry) then we would attempt a standard SMB delete, but |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 918 | * if that fails we can not attempt the fall back mechanisms on EACCESS |
| 919 | * but will return the EACCESS to the caller. Note that the VFS does not call |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 920 | * unlink on negative dentries currently. |
| 921 | */ |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 922 | int cifs_unlink(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
| 924 | int rc = 0; |
| 925 | int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | char *full_path = NULL; |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 927 | struct inode *inode = dentry->d_inode; |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 928 | struct cifsInodeInfo *cifs_inode; |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 929 | struct super_block *sb = dir->i_sb; |
| 930 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 931 | struct cifsTconInfo *tcon = cifs_sb->tcon; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 932 | struct iattr *attrs = NULL; |
| 933 | __u32 dosattr = 0, origattr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 935 | cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
| 937 | xid = GetXid(); |
| 938 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 939 | /* Unlink can be called from rename so we can not take the |
| 940 | * sb->s_vfs_rename_mutex here */ |
| 941 | full_path = build_path_from_dentry(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 943 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 945 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 947 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 948 | if ((tcon->ses->capabilities & CAP_UNIX) && |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 949 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 950 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
| 951 | rc = CIFSPOSIXDelFile(xid, tcon, full_path, |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 952 | SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls, |
| 953 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 954 | cFYI(1, ("posix del rc %d", rc)); |
| 955 | if ((rc == 0) || (rc == -ENOENT)) |
| 956 | goto psx_del_no_retry; |
| 957 | } |
| 958 | |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 959 | retry_std_delete: |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 960 | rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 961 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 962 | |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 963 | psx_del_no_retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (!rc) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 965 | if (inode) |
| 966 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } else if (rc == -ENOENT) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 968 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } else if (rc == -ETXTBSY) { |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 970 | rc = cifs_rename_pending_delete(full_path, dentry, xid); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 971 | if (rc == 0) |
| 972 | drop_nlink(inode); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 973 | } else if ((rc == -EACCES) && (dosattr == 0) && inode) { |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 974 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
| 975 | if (attrs == NULL) { |
| 976 | rc = -ENOMEM; |
| 977 | goto out_reval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 979 | |
| 980 | /* try to reset dos attributes */ |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 981 | cifs_inode = CIFS_I(inode); |
| 982 | origattr = cifs_inode->cifsAttrs; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 983 | if (origattr == 0) |
| 984 | origattr |= ATTR_NORMAL; |
| 985 | dosattr = origattr & ~ATTR_READONLY; |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 986 | if (dosattr == 0) |
| 987 | dosattr |= ATTR_NORMAL; |
| 988 | dosattr |= ATTR_HIDDEN; |
| 989 | |
| 990 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 991 | if (rc != 0) |
| 992 | goto out_reval; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 993 | |
| 994 | goto retry_std_delete; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 996 | |
| 997 | /* undo the setattr if we errored out and it's needed */ |
| 998 | if (rc != 0 && dosattr != 0) |
| 999 | cifs_set_file_info(inode, attrs, xid, full_path, origattr); |
| 1000 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1001 | out_reval: |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1002 | if (inode) { |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1003 | cifs_inode = CIFS_I(inode); |
| 1004 | cifs_inode->time = 0; /* will force revalidate to get info |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1005 | when needed */ |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1006 | inode->i_ctime = current_fs_time(sb); |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1007 | } |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1008 | dir->i_ctime = dir->i_mtime = current_fs_time(sb); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1009 | cifs_inode = CIFS_I(dir); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1010 | CIFS_I(dir)->time = 0; /* force revalidate of dir as well */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
| 1012 | kfree(full_path); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1013 | kfree(attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | FreeXid(xid); |
| 1015 | return rc; |
| 1016 | } |
| 1017 | |
| 1018 | int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode) |
| 1019 | { |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1020 | int rc = 0, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | int xid; |
| 1022 | struct cifs_sb_info *cifs_sb; |
| 1023 | struct cifsTconInfo *pTcon; |
| 1024 | char *full_path = NULL; |
| 1025 | struct inode *newinode = NULL; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1026 | struct cifs_fattr fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1028 | cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | xid = GetXid(); |
| 1031 | |
| 1032 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1033 | pTcon = cifs_sb->tcon; |
| 1034 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1035 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1037 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1039 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1041 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1042 | if ((pTcon->ses->capabilities & CAP_UNIX) && |
| 1043 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1044 | le64_to_cpu(pTcon->fsUnixInfo.Capability))) { |
| 1045 | u32 oplock = 0; |
Steve French | f6d0998 | 2008-01-08 23:18:22 +0000 | [diff] [blame] | 1046 | FILE_UNIX_BASIC_INFO *pInfo = |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1047 | kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1048 | if (pInfo == NULL) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1049 | rc = -ENOMEM; |
| 1050 | goto mkdir_out; |
| 1051 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1052 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 1053 | mode &= ~current_umask(); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1054 | rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT, |
| 1055 | mode, NULL /* netfid */, pInfo, &oplock, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1056 | full_path, cifs_sb->local_nls, |
| 1057 | cifs_sb->mnt_cifs_flags & |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1058 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | c45d707 | 2007-09-17 02:04:21 +0000 | [diff] [blame] | 1059 | if (rc == -EOPNOTSUPP) { |
| 1060 | kfree(pInfo); |
| 1061 | goto mkdir_retry_old; |
| 1062 | } else if (rc) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1063 | cFYI(1, ("posix mkdir returned 0x%x", rc)); |
| 1064 | d_drop(direntry); |
| 1065 | } else { |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1066 | if (pInfo->Type == cpu_to_le32(-1)) { |
| 1067 | /* no return info, go query for it */ |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1068 | kfree(pInfo); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1069 | goto mkdir_get_info; |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1070 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1071 | /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need |
| 1072 | to set uid/gid */ |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1073 | inc_nlink(inode); |
| 1074 | if (pTcon->nocase) |
| 1075 | direntry->d_op = &cifs_ci_dentry_ops; |
| 1076 | else |
| 1077 | direntry->d_op = &cifs_dentry_ops; |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1078 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1079 | cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb); |
| 1080 | newinode = cifs_iget(inode->i_sb, &fattr); |
| 1081 | if (!newinode) { |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1082 | kfree(pInfo); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1083 | goto mkdir_get_info; |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1084 | } |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1085 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1086 | d_instantiate(direntry, newinode); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1087 | |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1088 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1089 | cFYI(1, ("instantiated dentry %p %s to inode %p", |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1090 | direntry, direntry->d_name.name, newinode)); |
| 1091 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1092 | if (newinode->i_nlink != 2) |
| 1093 | cFYI(1, ("unexpected number of links %d", |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1094 | newinode->i_nlink)); |
| 1095 | #endif |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1096 | } |
| 1097 | kfree(pInfo); |
| 1098 | goto mkdir_out; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1099 | } |
Steve French | c45d707 | 2007-09-17 02:04:21 +0000 | [diff] [blame] | 1100 | mkdir_retry_old: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 1102 | rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls, |
| 1103 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | if (rc) { |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 1105 | cFYI(1, ("cifs_mkdir returned 0x%x", rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | d_drop(direntry); |
| 1107 | } else { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1108 | mkdir_get_info: |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1109 | inc_nlink(inode); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1110 | if (pTcon->unix_ext) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1112 | inode->i_sb, xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | else |
| 1114 | rc = cifs_get_inode_info(&newinode, full_path, NULL, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 1115 | inode->i_sb, xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 1117 | if (pTcon->nocase) |
| 1118 | direntry->d_op = &cifs_ci_dentry_ops; |
| 1119 | else |
| 1120 | direntry->d_op = &cifs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | d_instantiate(direntry, newinode); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1122 | /* setting nlink not necessary except in cases where we |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1123 | * failed to get it from the server or was set bogus */ |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1124 | if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2)) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1125 | direntry->d_inode->i_nlink = 2; |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1126 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 1127 | mode &= ~current_umask(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1128 | /* must turn on setgid bit if parent dir has it */ |
| 1129 | if (inode->i_mode & S_ISGID) |
| 1130 | mode |= S_ISGID; |
| 1131 | |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1132 | if (pTcon->unix_ext) { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1133 | struct cifs_unix_set_info_args args = { |
| 1134 | .mode = mode, |
| 1135 | .ctime = NO_CHANGE_64, |
| 1136 | .atime = NO_CHANGE_64, |
| 1137 | .mtime = NO_CHANGE_64, |
| 1138 | .device = 0, |
| 1139 | }; |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 1140 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1141 | args.uid = (__u64)current_fsuid(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1142 | if (inode->i_mode & S_ISGID) |
| 1143 | args.gid = (__u64)inode->i_gid; |
| 1144 | else |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1145 | args.gid = (__u64)current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | } else { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1147 | args.uid = NO_CHANGE_64; |
| 1148 | args.gid = NO_CHANGE_64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 1150 | CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args, |
| 1151 | cifs_sb->local_nls, |
| 1152 | cifs_sb->mnt_cifs_flags & |
| 1153 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 3ce53fc | 2007-06-08 14:55:14 +0000 | [diff] [blame] | 1154 | } else { |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1155 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) && |
| 1156 | (mode & S_IWUGO) == 0) { |
| 1157 | FILE_BASIC_INFO pInfo; |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1158 | struct cifsInodeInfo *cifsInode; |
| 1159 | u32 dosattrs; |
| 1160 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1161 | memset(&pInfo, 0, sizeof(pInfo)); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1162 | cifsInode = CIFS_I(newinode); |
| 1163 | dosattrs = cifsInode->cifsAttrs|ATTR_READONLY; |
| 1164 | pInfo.Attributes = cpu_to_le32(dosattrs); |
| 1165 | tmprc = CIFSSMBSetPathInfo(xid, pTcon, |
| 1166 | full_path, &pInfo, |
| 1167 | cifs_sb->local_nls, |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1168 | cifs_sb->mnt_cifs_flags & |
| 1169 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1170 | if (tmprc == 0) |
| 1171 | cifsInode->cifsAttrs = dosattrs; |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1172 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1173 | if (direntry->d_inode) { |
Jeff Layton | b0fd30d | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1174 | if (cifs_sb->mnt_cifs_flags & |
| 1175 | CIFS_MOUNT_DYNPERM) |
| 1176 | direntry->d_inode->i_mode = |
| 1177 | (mode | S_IFDIR); |
Steve French | 4e94a10 | 2008-05-23 18:22:46 +0000 | [diff] [blame] | 1178 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1179 | if (cifs_sb->mnt_cifs_flags & |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1180 | CIFS_MOUNT_SET_UID) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1181 | direntry->d_inode->i_uid = |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1182 | current_fsuid(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1183 | if (inode->i_mode & S_ISGID) |
| 1184 | direntry->d_inode->i_gid = |
| 1185 | inode->i_gid; |
| 1186 | else |
| 1187 | direntry->d_inode->i_gid = |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1188 | current_fsgid(); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1189 | } |
| 1190 | } |
Steve French | 2a138ebb | 2005-11-29 21:22:19 -0800 | [diff] [blame] | 1191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1193 | mkdir_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | kfree(full_path); |
| 1195 | FreeXid(xid); |
| 1196 | return rc; |
| 1197 | } |
| 1198 | |
| 1199 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) |
| 1200 | { |
| 1201 | int rc = 0; |
| 1202 | int xid; |
| 1203 | struct cifs_sb_info *cifs_sb; |
| 1204 | struct cifsTconInfo *pTcon; |
| 1205 | char *full_path = NULL; |
| 1206 | struct cifsInodeInfo *cifsInode; |
| 1207 | |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 1208 | cFYI(1, ("cifs_rmdir, inode = 0x%p", inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
| 1210 | xid = GetXid(); |
| 1211 | |
| 1212 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1213 | pTcon = cifs_sb->tcon; |
| 1214 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1215 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1217 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1219 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 1222 | rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls, |
| 1223 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
| 1225 | if (!rc) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1226 | drop_nlink(inode); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1227 | spin_lock(&direntry->d_inode->i_lock); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1228 | i_size_write(direntry->d_inode, 0); |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 1229 | clear_nlink(direntry->d_inode); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1230 | spin_unlock(&direntry->d_inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | cifsInode = CIFS_I(direntry->d_inode); |
| 1234 | cifsInode->time = 0; /* force revalidate to go get info when |
| 1235 | needed */ |
Steve French | 42c2454 | 2009-01-13 22:03:55 +0000 | [diff] [blame] | 1236 | |
| 1237 | cifsInode = CIFS_I(inode); |
| 1238 | cifsInode->time = 0; /* force revalidate to get parent dir info |
| 1239 | since cached search results now invalid */ |
| 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime = |
| 1242 | current_fs_time(inode->i_sb); |
| 1243 | |
| 1244 | kfree(full_path); |
| 1245 | FreeXid(xid); |
| 1246 | return rc; |
| 1247 | } |
| 1248 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1249 | static int |
| 1250 | cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath, |
| 1251 | struct dentry *to_dentry, const char *toPath) |
| 1252 | { |
| 1253 | struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb); |
| 1254 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1255 | __u16 srcfid; |
| 1256 | int oplock, rc; |
| 1257 | |
| 1258 | /* try path-based rename first */ |
| 1259 | rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls, |
| 1260 | cifs_sb->mnt_cifs_flags & |
| 1261 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1262 | |
| 1263 | /* |
| 1264 | * don't bother with rename by filehandle unless file is busy and |
| 1265 | * source Note that cross directory moves do not work with |
| 1266 | * rename by filehandle to various Windows servers. |
| 1267 | */ |
| 1268 | if (rc == 0 || rc != -ETXTBSY) |
| 1269 | return rc; |
| 1270 | |
| 1271 | /* open the file to be renamed -- we need DELETE perms */ |
| 1272 | rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE, |
| 1273 | CREATE_NOT_DIR, &srcfid, &oplock, NULL, |
| 1274 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 1275 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1276 | |
| 1277 | if (rc == 0) { |
| 1278 | rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid, |
| 1279 | (const char *) to_dentry->d_name.name, |
| 1280 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 1281 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1282 | |
| 1283 | CIFSSMBClose(xid, pTcon, srcfid); |
| 1284 | } |
| 1285 | |
| 1286 | return rc; |
| 1287 | } |
| 1288 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1289 | int cifs_rename(struct inode *source_dir, struct dentry *source_dentry, |
| 1290 | struct inode *target_dir, struct dentry *target_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1292 | char *fromName = NULL; |
| 1293 | char *toName = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | struct cifs_sb_info *cifs_sb_source; |
| 1295 | struct cifs_sb_info *cifs_sb_target; |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1296 | struct cifsTconInfo *tcon; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1297 | FILE_UNIX_BASIC_INFO *info_buf_source = NULL; |
| 1298 | FILE_UNIX_BASIC_INFO *info_buf_target; |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1299 | int xid, rc, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1301 | cifs_sb_target = CIFS_SB(target_dir->i_sb); |
| 1302 | cifs_sb_source = CIFS_SB(source_dir->i_sb); |
| 1303 | tcon = cifs_sb_source->tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1305 | xid = GetXid(); |
| 1306 | |
| 1307 | /* |
| 1308 | * BB: this might be allowed if same server, but different share. |
| 1309 | * Consider adding support for this |
| 1310 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1311 | if (tcon != cifs_sb_target->tcon) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1312 | rc = -EXDEV; |
| 1313 | goto cifs_rename_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1316 | /* |
| 1317 | * we already have the rename sem so we do not need to |
| 1318 | * grab it again here to protect the path integrity |
| 1319 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1320 | fromName = build_path_from_dentry(source_dentry); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1321 | if (fromName == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | rc = -ENOMEM; |
| 1323 | goto cifs_rename_exit; |
| 1324 | } |
| 1325 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1326 | toName = build_path_from_dentry(target_dentry); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1327 | if (toName == NULL) { |
| 1328 | rc = -ENOMEM; |
| 1329 | goto cifs_rename_exit; |
| 1330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1332 | rc = cifs_do_rename(xid, source_dentry, fromName, |
| 1333 | target_dentry, toName); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1334 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1335 | if (rc == -EEXIST && tcon->unix_ext) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1336 | /* |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1337 | * Are src and dst hardlinks of same inode? We can |
| 1338 | * only tell with unix extensions enabled |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1339 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1340 | info_buf_source = |
| 1341 | kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), |
| 1342 | GFP_KERNEL); |
| 1343 | if (info_buf_source == NULL) { |
| 1344 | rc = -ENOMEM; |
| 1345 | goto cifs_rename_exit; |
| 1346 | } |
| 1347 | |
| 1348 | info_buf_target = info_buf_source + 1; |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1349 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName, |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1350 | info_buf_source, |
| 1351 | cifs_sb_source->local_nls, |
| 1352 | cifs_sb_source->mnt_cifs_flags & |
| 1353 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1354 | if (tmprc != 0) |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1355 | goto unlink_target; |
| 1356 | |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1357 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1358 | toName, info_buf_target, |
| 1359 | cifs_sb_target->local_nls, |
| 1360 | /* remap based on source sb */ |
| 1361 | cifs_sb_source->mnt_cifs_flags & |
| 1362 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1363 | |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1364 | if (tmprc == 0 && (info_buf_source->UniqueId == |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1365 | info_buf_target->UniqueId)) { |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1366 | /* same file, POSIX says that this is a noop */ |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1367 | rc = 0; |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1368 | goto cifs_rename_exit; |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1369 | } |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1370 | } /* else ... BB we could add the same check for Windows by |
| 1371 | checking the UniqueId via FILE_INTERNAL_INFO */ |
| 1372 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1373 | unlink_target: |
Jeff Layton | fc6f394 | 2009-04-17 11:45:30 -0400 | [diff] [blame] | 1374 | /* Try unlinking the target dentry if it's not negative */ |
| 1375 | if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) { |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1376 | tmprc = cifs_unlink(target_dir, target_dentry); |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1377 | if (tmprc) |
| 1378 | goto cifs_rename_exit; |
| 1379 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1380 | rc = cifs_do_rename(xid, source_dentry, fromName, |
| 1381 | target_dentry, toName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | cifs_rename_exit: |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1385 | kfree(info_buf_source); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | kfree(fromName); |
| 1387 | kfree(toName); |
| 1388 | FreeXid(xid); |
| 1389 | return rc; |
| 1390 | } |
| 1391 | |
| 1392 | int cifs_revalidate(struct dentry *direntry) |
| 1393 | { |
| 1394 | int xid; |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1395 | int rc = 0, wbrc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | char *full_path; |
| 1397 | struct cifs_sb_info *cifs_sb; |
| 1398 | struct cifsInodeInfo *cifsInode; |
| 1399 | loff_t local_size; |
| 1400 | struct timespec local_mtime; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 1401 | bool invalidate_inode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | if (direntry->d_inode == NULL) |
| 1404 | return -ENOENT; |
| 1405 | |
| 1406 | cifsInode = CIFS_I(direntry->d_inode); |
| 1407 | |
| 1408 | if (cifsInode == NULL) |
| 1409 | return -ENOENT; |
| 1410 | |
| 1411 | /* no sense revalidating inode info on file that no one can write */ |
| 1412 | if (CIFS_I(direntry->d_inode)->clientCanCacheRead) |
| 1413 | return rc; |
| 1414 | |
| 1415 | xid = GetXid(); |
| 1416 | |
| 1417 | cifs_sb = CIFS_SB(direntry->d_sb); |
| 1418 | |
| 1419 | /* can not safely grab the rename sem here if rename calls revalidate |
| 1420 | since that would deadlock */ |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1421 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1423 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1425 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } |
| 1427 | cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld " |
| 1428 | "jiffies %ld", full_path, direntry->d_inode, |
| 1429 | direntry->d_inode->i_count.counter, direntry, |
| 1430 | direntry->d_time, jiffies)); |
| 1431 | |
| 1432 | if (cifsInode->time == 0) { |
| 1433 | /* was set to zero previously to force revalidate */ |
| 1434 | } else if (time_before(jiffies, cifsInode->time + HZ) && |
| 1435 | lookupCacheEnabled) { |
| 1436 | if ((S_ISREG(direntry->d_inode->i_mode) == 0) || |
| 1437 | (direntry->d_inode->i_nlink == 1)) { |
| 1438 | kfree(full_path); |
| 1439 | FreeXid(xid); |
| 1440 | return rc; |
| 1441 | } else { |
| 1442 | cFYI(1, ("Have to revalidate file due to hardlinks")); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /* save mtime and size */ |
| 1447 | local_mtime = direntry->d_inode->i_mtime; |
| 1448 | local_size = direntry->d_inode->i_size; |
| 1449 | |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1450 | if (cifs_sb->tcon->unix_ext) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1452 | direntry->d_sb, xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | if (rc) { |
| 1454 | cFYI(1, ("error on getting revalidate info %d", rc)); |
| 1455 | /* if (rc != -ENOENT) |
| 1456 | rc = 0; */ /* BB should we cache info on |
| 1457 | certain errors? */ |
| 1458 | } |
| 1459 | } else { |
| 1460 | rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 1461 | direntry->d_sb, xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | if (rc) { |
| 1463 | cFYI(1, ("error on getting revalidate info %d", rc)); |
| 1464 | /* if (rc != -ENOENT) |
| 1465 | rc = 0; */ /* BB should we cache info on |
| 1466 | certain errors? */ |
| 1467 | } |
| 1468 | } |
| 1469 | /* should we remap certain errors, access denied?, to zero */ |
| 1470 | |
| 1471 | /* if not oplocked, we invalidate inode pages if mtime or file size |
| 1472 | had changed on server */ |
| 1473 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1474 | if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | (local_size == direntry->d_inode->i_size)) { |
| 1476 | cFYI(1, ("cifs_revalidate - inode unchanged")); |
| 1477 | } else { |
| 1478 | /* file may have changed on server */ |
| 1479 | if (cifsInode->clientCanCacheRead) { |
| 1480 | /* no need to invalidate inode pages since we were the |
| 1481 | only ones who could have modified the file and the |
| 1482 | server copy is staler than ours */ |
| 1483 | } else { |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 1484 | invalidate_inode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | /* can not grab this sem since kernel filesys locking documentation |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1489 | indicates i_mutex may be taken by the kernel on lookup and rename |
| 1490 | which could deadlock if we grab the i_mutex here as well */ |
| 1491 | /* mutex_lock(&direntry->d_inode->i_mutex);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | /* need to write out dirty pages here */ |
| 1493 | if (direntry->d_inode->i_mapping) { |
| 1494 | /* do we need to lock inode until after invalidate completes |
| 1495 | below? */ |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1496 | wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping); |
| 1497 | if (wbrc) |
| 1498 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
| 1500 | if (invalidate_inode) { |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1501 | /* shrink_dcache not necessary now that cifs dentry ops |
| 1502 | are exported for negative dentries */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1503 | /* if (S_ISDIR(direntry->d_inode->i_mode)) |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1504 | shrink_dcache_parent(direntry); */ |
| 1505 | if (S_ISREG(direntry->d_inode->i_mode)) { |
Suresh Jayaraman | 9e96af8 | 2008-08-05 14:38:40 +0530 | [diff] [blame] | 1506 | if (direntry->d_inode->i_mapping) { |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1507 | wbrc = filemap_fdatawait(direntry->d_inode->i_mapping); |
| 1508 | if (wbrc) |
| 1509 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; |
Suresh Jayaraman | 9e96af8 | 2008-08-05 14:38:40 +0530 | [diff] [blame] | 1510 | } |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1511 | /* may eventually have to do this for open files too */ |
| 1512 | if (list_empty(&(cifsInode->openFileList))) { |
| 1513 | /* changed on server - flush read ahead pages */ |
| 1514 | cFYI(1, ("Invalidating read ahead data on " |
| 1515 | "closed file")); |
| 1516 | invalidate_remote_inode(direntry->d_inode); |
| 1517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1520 | /* mutex_unlock(&direntry->d_inode->i_mutex); */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | kfree(full_path); |
| 1523 | FreeXid(xid); |
| 1524 | return rc; |
| 1525 | } |
| 1526 | |
| 1527 | int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1528 | struct kstat *stat) |
| 1529 | { |
| 1530 | int err = cifs_revalidate(dentry); |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1531 | if (!err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | generic_fillattr(dentry->d_inode, stat); |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1533 | stat->blksize = CIFS_MAX_MSGSIZE; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 1534 | stat->ino = CIFS_I(dentry->d_inode)->uniqueid; |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | return err; |
| 1537 | } |
| 1538 | |
| 1539 | static int cifs_truncate_page(struct address_space *mapping, loff_t from) |
| 1540 | { |
| 1541 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 1542 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 1543 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | int rc = 0; |
| 1545 | |
| 1546 | page = grab_cache_page(mapping, index); |
| 1547 | if (!page) |
| 1548 | return -ENOMEM; |
| 1549 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 1550 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | unlock_page(page); |
| 1552 | page_cache_release(page); |
| 1553 | return rc; |
| 1554 | } |
| 1555 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1556 | static int cifs_vmtruncate(struct inode *inode, loff_t offset) |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1557 | { |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1558 | loff_t oldsize; |
| 1559 | int err; |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1560 | |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1561 | spin_lock(&inode->i_lock); |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1562 | err = inode_newsize_ok(inode, offset); |
| 1563 | if (err) { |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1564 | spin_unlock(&inode->i_lock); |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1565 | goto out; |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1566 | } |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1567 | |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1568 | oldsize = inode->i_size; |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1569 | i_size_write(inode, offset); |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1570 | spin_unlock(&inode->i_lock); |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1571 | truncate_pagecache(inode, oldsize, offset); |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 1572 | if (inode->i_op->truncate) |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1573 | inode->i_op->truncate(inode); |
npiggin@suse.de | c08d3b0 | 2009-08-21 02:35:06 +1000 | [diff] [blame] | 1574 | out: |
| 1575 | return err; |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1578 | static int |
| 1579 | cifs_set_file_size(struct inode *inode, struct iattr *attrs, |
| 1580 | int xid, char *full_path) |
| 1581 | { |
| 1582 | int rc; |
| 1583 | struct cifsFileInfo *open_file; |
| 1584 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1585 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1586 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1587 | |
| 1588 | /* |
| 1589 | * To avoid spurious oplock breaks from server, in the case of |
| 1590 | * inodes that we already have open, avoid doing path based |
| 1591 | * setting of file size if we can do it by handle. |
| 1592 | * This keeps our caching token (oplock) and avoids timeouts |
| 1593 | * when the local oplock break takes longer to flush |
| 1594 | * writebehind data than the SMB timeout for the SetPathInfo |
| 1595 | * request would allow |
| 1596 | */ |
| 1597 | open_file = find_writable_file(cifsInode); |
| 1598 | if (open_file) { |
| 1599 | __u16 nfid = open_file->netfid; |
| 1600 | __u32 npid = open_file->pid; |
| 1601 | rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid, |
| 1602 | npid, false); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 1603 | cifsFileInfo_put(open_file); |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1604 | cFYI(1, ("SetFSize for attrs rc = %d", rc)); |
| 1605 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 1606 | unsigned int bytes_written; |
| 1607 | rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size, |
| 1608 | &bytes_written, NULL, NULL, 1); |
| 1609 | cFYI(1, ("Wrt seteof rc %d", rc)); |
| 1610 | } |
| 1611 | } else |
| 1612 | rc = -EINVAL; |
| 1613 | |
| 1614 | if (rc != 0) { |
| 1615 | /* Set file size by pathname rather than by handle |
| 1616 | either because no valid, writeable file handle for |
| 1617 | it was found or because there was an error setting |
| 1618 | it by handle */ |
| 1619 | rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, |
| 1620 | false, cifs_sb->local_nls, |
| 1621 | cifs_sb->mnt_cifs_flags & |
| 1622 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1623 | cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc)); |
| 1624 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 1625 | __u16 netfid; |
| 1626 | int oplock = 0; |
| 1627 | |
| 1628 | rc = SMBLegacyOpen(xid, pTcon, full_path, |
| 1629 | FILE_OPEN, GENERIC_WRITE, |
| 1630 | CREATE_NOT_DIR, &netfid, &oplock, NULL, |
| 1631 | cifs_sb->local_nls, |
| 1632 | cifs_sb->mnt_cifs_flags & |
| 1633 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1634 | if (rc == 0) { |
| 1635 | unsigned int bytes_written; |
| 1636 | rc = CIFSSMBWrite(xid, pTcon, netfid, 0, |
| 1637 | attrs->ia_size, |
| 1638 | &bytes_written, NULL, |
| 1639 | NULL, 1); |
| 1640 | cFYI(1, ("wrt seteof rc %d", rc)); |
| 1641 | CIFSSMBClose(xid, pTcon, netfid); |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | if (rc == 0) { |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1647 | cifsInode->server_eof = attrs->ia_size; |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1648 | rc = cifs_vmtruncate(inode, attrs->ia_size); |
| 1649 | cifs_truncate_page(inode->i_mapping, inode->i_size); |
| 1650 | } |
| 1651 | |
| 1652 | return rc; |
| 1653 | } |
| 1654 | |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1655 | static int |
| 1656 | cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) |
| 1657 | { |
| 1658 | int rc; |
| 1659 | int xid; |
| 1660 | char *full_path = NULL; |
| 1661 | struct inode *inode = direntry->d_inode; |
| 1662 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1663 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1664 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1665 | struct cifs_unix_set_info_args *args = NULL; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 1666 | struct cifsFileInfo *open_file; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1667 | |
| 1668 | cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x", |
| 1669 | direntry->d_name.name, attrs->ia_valid)); |
| 1670 | |
| 1671 | xid = GetXid(); |
| 1672 | |
| 1673 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { |
| 1674 | /* check if we have permission to change attrs */ |
| 1675 | rc = inode_change_ok(inode, attrs); |
| 1676 | if (rc < 0) |
| 1677 | goto out; |
| 1678 | else |
| 1679 | rc = 0; |
| 1680 | } |
| 1681 | |
| 1682 | full_path = build_path_from_dentry(direntry); |
| 1683 | if (full_path == NULL) { |
| 1684 | rc = -ENOMEM; |
| 1685 | goto out; |
| 1686 | } |
| 1687 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 1688 | /* |
| 1689 | * Attempt to flush data before changing attributes. We need to do |
| 1690 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 1691 | * ownership or mode then we may also need to do this. Here, we take |
| 1692 | * the safe way out and just do the flush on all setattr requests. If |
| 1693 | * the flush returns error, store it to report later and continue. |
| 1694 | * |
| 1695 | * BB: This should be smarter. Why bother flushing pages that |
| 1696 | * will be truncated anyway? Also, should we error out here if |
| 1697 | * the flush returns error? |
| 1698 | */ |
| 1699 | rc = filemap_write_and_wait(inode->i_mapping); |
| 1700 | if (rc != 0) { |
| 1701 | cifsInode->write_behind_rc = rc; |
| 1702 | rc = 0; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | if (attrs->ia_valid & ATTR_SIZE) { |
| 1706 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 1707 | if (rc != 0) |
| 1708 | goto out; |
| 1709 | } |
| 1710 | |
| 1711 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 1712 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 1713 | attrs->ia_valid &= ~ATTR_MODE; |
| 1714 | |
| 1715 | args = kmalloc(sizeof(*args), GFP_KERNEL); |
| 1716 | if (args == NULL) { |
| 1717 | rc = -ENOMEM; |
| 1718 | goto out; |
| 1719 | } |
| 1720 | |
| 1721 | /* set up the struct */ |
| 1722 | if (attrs->ia_valid & ATTR_MODE) |
| 1723 | args->mode = attrs->ia_mode; |
| 1724 | else |
| 1725 | args->mode = NO_CHANGE_64; |
| 1726 | |
| 1727 | if (attrs->ia_valid & ATTR_UID) |
| 1728 | args->uid = attrs->ia_uid; |
| 1729 | else |
| 1730 | args->uid = NO_CHANGE_64; |
| 1731 | |
| 1732 | if (attrs->ia_valid & ATTR_GID) |
| 1733 | args->gid = attrs->ia_gid; |
| 1734 | else |
| 1735 | args->gid = NO_CHANGE_64; |
| 1736 | |
| 1737 | if (attrs->ia_valid & ATTR_ATIME) |
| 1738 | args->atime = cifs_UnixTimeToNT(attrs->ia_atime); |
| 1739 | else |
| 1740 | args->atime = NO_CHANGE_64; |
| 1741 | |
| 1742 | if (attrs->ia_valid & ATTR_MTIME) |
| 1743 | args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime); |
| 1744 | else |
| 1745 | args->mtime = NO_CHANGE_64; |
| 1746 | |
| 1747 | if (attrs->ia_valid & ATTR_CTIME) |
| 1748 | args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime); |
| 1749 | else |
| 1750 | args->ctime = NO_CHANGE_64; |
| 1751 | |
| 1752 | args->device = 0; |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 1753 | open_file = find_writable_file(cifsInode); |
| 1754 | if (open_file) { |
| 1755 | u16 nfid = open_file->netfid; |
| 1756 | u32 npid = open_file->pid; |
| 1757 | rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 1758 | cifsFileInfo_put(open_file); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 1759 | } else { |
| 1760 | rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args, |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 1761 | cifs_sb->local_nls, |
| 1762 | cifs_sb->mnt_cifs_flags & |
| 1763 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 3bbeeb3 | 2009-07-09 20:02:50 -0400 | [diff] [blame] | 1764 | } |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1765 | |
Steve French | ccd4bb1 | 2010-02-08 17:39:58 +0000 | [diff] [blame] | 1766 | if (!rc) { |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1767 | rc = inode_setattr(inode, attrs); |
Steve French | ccd4bb1 | 2010-02-08 17:39:58 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* force revalidate when any of these times are set since some |
| 1770 | of the fs types (eg ext3, fat) do not have fine enough |
| 1771 | time granularity to match protocol, and we do not have a |
| 1772 | a way (yet) to query the server fs's time granularity (and |
| 1773 | whether it rounds times down). |
| 1774 | */ |
| 1775 | if (!rc && (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))) |
| 1776 | cifsInode->time = 0; |
| 1777 | } |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1778 | out: |
| 1779 | kfree(args); |
| 1780 | kfree(full_path); |
| 1781 | FreeXid(xid); |
| 1782 | return rc; |
| 1783 | } |
| 1784 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1785 | static int |
| 1786 | cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | { |
| 1788 | int xid; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1789 | struct inode *inode = direntry->d_inode; |
| 1790 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1791 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | char *full_path = NULL; |
| 1793 | int rc = -EACCES; |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1794 | __u32 dosattr = 0; |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1795 | __u64 mode = NO_CHANGE_64; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | xid = GetXid(); |
| 1798 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1799 | cFYI(1, ("setattr on file %s attrs->iavalid 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | direntry->d_name.name, attrs->ia_valid)); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1801 | |
Steve French | 2a138ebb | 2005-11-29 21:22:19 -0800 | [diff] [blame] | 1802 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1803 | /* check if we have permission to change attrs */ |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 1804 | rc = inode_change_ok(inode, attrs); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1805 | if (rc < 0) { |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1806 | FreeXid(xid); |
| 1807 | return rc; |
| 1808 | } else |
| 1809 | rc = 0; |
| 1810 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1811 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1812 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1814 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1816 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 1819 | /* |
| 1820 | * Attempt to flush data before changing attributes. We need to do |
| 1821 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 1822 | * ownership or mode then we may also need to do this. Here, we take |
| 1823 | * the safe way out and just do the flush on all setattr requests. If |
| 1824 | * the flush returns error, store it to report later and continue. |
| 1825 | * |
| 1826 | * BB: This should be smarter. Why bother flushing pages that |
| 1827 | * will be truncated anyway? Also, should we error out here if |
| 1828 | * the flush returns error? |
| 1829 | */ |
| 1830 | rc = filemap_write_and_wait(inode->i_mapping); |
| 1831 | if (rc != 0) { |
| 1832 | cifsInode->write_behind_rc = rc; |
| 1833 | rc = 0; |
Steve French | 5053144 | 2008-03-14 19:21:31 +0000 | [diff] [blame] | 1834 | } |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1835 | |
Steve French | 5053144 | 2008-03-14 19:21:31 +0000 | [diff] [blame] | 1836 | if (attrs->ia_valid & ATTR_SIZE) { |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1837 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 1838 | if (rc != 0) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1839 | goto cifs_setattr_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | } |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1841 | |
| 1842 | /* |
| 1843 | * Without unix extensions we can't send ownership changes to the |
| 1844 | * server, so silently ignore them. This is consistent with how |
| 1845 | * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With |
| 1846 | * CIFSACL support + proper Windows to Unix idmapping, we may be |
| 1847 | * able to support this in the future. |
| 1848 | */ |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1849 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1850 | attrs->ia_valid &= ~(ATTR_UID | ATTR_GID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
Jeff Layton | d32c4f2 | 2007-10-18 03:05:22 -0700 | [diff] [blame] | 1852 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 1853 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 1854 | attrs->ia_valid &= ~ATTR_MODE; |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | if (attrs->ia_valid & ATTR_MODE) { |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1857 | cFYI(1, ("Mode changed to 0%o", attrs->ia_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | mode = attrs->ia_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1861 | if (attrs->ia_valid & ATTR_MODE) { |
Steve French | cdbce9c | 2005-11-19 21:04:52 -0800 | [diff] [blame] | 1862 | rc = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1863 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 1864 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 1865 | rc = mode_to_acl(inode, full_path, mode); |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1866 | else |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1867 | #endif |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1868 | if (((mode & S_IWUGO) == 0) && |
| 1869 | (cifsInode->cifsAttrs & ATTR_READONLY) == 0) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1870 | |
| 1871 | dosattr = cifsInode->cifsAttrs | ATTR_READONLY; |
| 1872 | |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1873 | /* fix up mode if we're not using dynperm */ |
| 1874 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0) |
| 1875 | attrs->ia_mode = inode->i_mode & ~S_IWUGO; |
| 1876 | } else if ((mode & S_IWUGO) && |
| 1877 | (cifsInode->cifsAttrs & ATTR_READONLY)) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1878 | |
| 1879 | dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY; |
| 1880 | /* Attributes of 0 are ignored */ |
| 1881 | if (dosattr == 0) |
| 1882 | dosattr |= ATTR_NORMAL; |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1883 | |
| 1884 | /* reset local inode permissions to normal */ |
| 1885 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 1886 | attrs->ia_mode &= ~(S_IALLUGO); |
| 1887 | if (S_ISDIR(inode->i_mode)) |
| 1888 | attrs->ia_mode |= |
| 1889 | cifs_sb->mnt_dir_mode; |
| 1890 | else |
| 1891 | attrs->ia_mode |= |
| 1892 | cifs_sb->mnt_file_mode; |
| 1893 | } |
| 1894 | } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 1895 | /* ignore mode change - ATTR_READONLY hasn't changed */ |
| 1896 | attrs->ia_valid &= ~ATTR_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1900 | if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) || |
| 1901 | ((attrs->ia_valid & ATTR_MODE) && dosattr)) { |
| 1902 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
| 1903 | /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1905 | /* Even if error on time set, no sense failing the call if |
| 1906 | the server would set the time to a reasonable value anyway, |
| 1907 | and this check ensures that we are not being called from |
| 1908 | sys_utimes in which case we ought to fail the call back to |
| 1909 | the user when the server rejects the call */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1910 | if ((rc) && (attrs->ia_valid & |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1911 | (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1912 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /* do not need local check to inode_check_ok since the server does |
| 1916 | that */ |
| 1917 | if (!rc) |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 1918 | rc = inode_setattr(inode, attrs); |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1919 | cifs_setattr_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | kfree(full_path); |
| 1921 | FreeXid(xid); |
| 1922 | return rc; |
| 1923 | } |
| 1924 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1925 | int |
| 1926 | cifs_setattr(struct dentry *direntry, struct iattr *attrs) |
| 1927 | { |
| 1928 | struct inode *inode = direntry->d_inode; |
| 1929 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1930 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1931 | |
| 1932 | if (pTcon->unix_ext) |
| 1933 | return cifs_setattr_unix(direntry, attrs); |
| 1934 | |
| 1935 | return cifs_setattr_nounix(direntry, attrs); |
| 1936 | |
| 1937 | /* BB: add cifs_setattr_legacy for really old servers */ |
| 1938 | } |
| 1939 | |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 1940 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | void cifs_delete_inode(struct inode *inode) |
| 1942 | { |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 1943 | cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | /* may have to add back in if and when safe distributed caching of |
| 1945 | directories added e.g. via FindNotify */ |
| 1946 | } |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 1947 | #endif |