Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/smb2inode.c |
| 3 | * |
| 4 | * Copyright (C) International Business Machines Corp., 2002, 2011 |
| 5 | * Etersoft, 2012 |
| 6 | * Author(s): Pavel Shilovsky (pshilovsky@samba.org), |
| 7 | * Steve French (sfrench@us.ibm.com) |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/stat.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/pagemap.h> |
| 27 | #include <asm/div64.h> |
| 28 | #include "cifsfs.h" |
| 29 | #include "cifspdu.h" |
| 30 | #include "cifsglob.h" |
| 31 | #include "cifsproto.h" |
| 32 | #include "cifs_debug.h" |
| 33 | #include "cifs_fs_sb.h" |
| 34 | #include "cifs_unicode.h" |
| 35 | #include "fscache.h" |
| 36 | #include "smb2glob.h" |
| 37 | #include "smb2pdu.h" |
| 38 | #include "smb2proto.h" |
| 39 | |
| 40 | static int |
| 41 | smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon, |
| 42 | struct cifs_sb_info *cifs_sb, const char *full_path, |
| 43 | __u32 desired_access, __u32 create_disposition, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 44 | __u32 create_options, void *data, int command) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 45 | { |
| 46 | int rc, tmprc = 0; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 47 | __le16 *utf16_path; |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 48 | __u8 oplock = SMB2_OPLOCK_LEVEL_NONE; |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 49 | struct cifs_open_parms oparms; |
| 50 | struct cifs_fid fid; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 51 | |
| 52 | utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb); |
| 53 | if (!utf16_path) |
| 54 | return -ENOMEM; |
| 55 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 56 | oparms.tcon = tcon; |
| 57 | oparms.desired_access = desired_access; |
| 58 | oparms.disposition = create_disposition; |
| 59 | oparms.create_options = create_options; |
| 60 | oparms.fid = &fid; |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 61 | oparms.reconnect = false; |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 62 | |
Pavel Shilovsky | b42bf88 | 2013-08-14 19:25:21 +0400 | [diff] [blame] | 63 | rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 64 | if (rc) { |
| 65 | kfree(utf16_path); |
| 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | switch (command) { |
| 70 | case SMB2_OP_DELETE: |
| 71 | break; |
| 72 | case SMB2_OP_QUERY_INFO: |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 73 | tmprc = SMB2_query_info(xid, tcon, fid.persistent_fid, |
| 74 | fid.volatile_fid, |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 75 | (struct smb2_file_all_info *)data); |
| 76 | break; |
| 77 | case SMB2_OP_MKDIR: |
| 78 | /* |
| 79 | * Directories are created through parameters in the |
| 80 | * SMB2_open() call. |
| 81 | */ |
| 82 | break; |
Steve French | 897fba1 | 2016-05-12 21:20:36 -0500 | [diff] [blame] | 83 | case SMB2_OP_RMDIR: |
| 84 | tmprc = SMB2_rmdir(xid, tcon, fid.persistent_fid, |
| 85 | fid.volatile_fid); |
| 86 | break; |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 87 | case SMB2_OP_RENAME: |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 88 | tmprc = SMB2_rename(xid, tcon, fid.persistent_fid, |
| 89 | fid.volatile_fid, (__le16 *)data); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 90 | break; |
Pavel Shilovsky | 568798c | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 91 | case SMB2_OP_HARDLINK: |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 92 | tmprc = SMB2_set_hardlink(xid, tcon, fid.persistent_fid, |
| 93 | fid.volatile_fid, (__le16 *)data); |
Pavel Shilovsky | 568798c | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 94 | break; |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 95 | case SMB2_OP_SET_EOF: |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 96 | tmprc = SMB2_set_eof(xid, tcon, fid.persistent_fid, |
| 97 | fid.volatile_fid, current->tgid, |
Steve French | f29ebb4 | 2014-07-19 21:44:58 -0500 | [diff] [blame] | 98 | (__le64 *)data, false); |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 99 | break; |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 100 | case SMB2_OP_SET_INFO: |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 101 | tmprc = SMB2_set_info(xid, tcon, fid.persistent_fid, |
| 102 | fid.volatile_fid, |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 103 | (FILE_BASIC_INFO *)data); |
| 104 | break; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 105 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 106 | cifs_dbg(VFS, "Invalid command\n"); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | |
Pavel Shilovsky | 064f604 | 2013-07-09 18:20:30 +0400 | [diff] [blame] | 110 | rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 111 | if (tmprc) |
| 112 | rc = tmprc; |
| 113 | kfree(utf16_path); |
| 114 | return rc; |
| 115 | } |
| 116 | |
Pavel Shilovsky | f0df737 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 117 | void |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 118 | move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src) |
| 119 | { |
| 120 | memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src); |
| 121 | dst->CurrentByteOffset = src->CurrentByteOffset; |
| 122 | dst->Mode = src->Mode; |
| 123 | dst->AlignmentRequirement = src->AlignmentRequirement; |
| 124 | dst->IndexNumber1 = 0; /* we don't use it */ |
| 125 | } |
| 126 | |
| 127 | int |
| 128 | smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon, |
| 129 | struct cifs_sb_info *cifs_sb, const char *full_path, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 130 | FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink) |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 131 | { |
| 132 | int rc; |
| 133 | struct smb2_file_all_info *smb2_data; |
| 134 | |
| 135 | *adjust_tz = false; |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 136 | *symlink = false; |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 137 | |
Pavel Shilovsky | 1bbe499 | 2014-08-22 13:32:11 +0400 | [diff] [blame] | 138 | smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 139 | GFP_KERNEL); |
| 140 | if (smb2_data == NULL) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path, |
Pavel Shilovsky | eb85d94b | 2013-10-23 17:49:47 +0400 | [diff] [blame] | 144 | FILE_READ_ATTRIBUTES, FILE_OPEN, 0, |
| 145 | smb2_data, SMB2_OP_QUERY_INFO); |
| 146 | if (rc == -EOPNOTSUPP) { |
| 147 | *symlink = true; |
| 148 | /* Failed on a symbolic link - query a reparse point info */ |
| 149 | rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path, |
| 150 | FILE_READ_ATTRIBUTES, FILE_OPEN, |
| 151 | OPEN_REPARSE_POINT, smb2_data, |
| 152 | SMB2_OP_QUERY_INFO); |
| 153 | } |
Pavel Shilovsky | be4cb9e | 2011-12-29 17:06:33 +0400 | [diff] [blame] | 154 | if (rc) |
| 155 | goto out; |
| 156 | |
| 157 | move_smb2_info_to_cifs(data, smb2_data); |
| 158 | out: |
| 159 | kfree(smb2_data); |
| 160 | return rc; |
| 161 | } |
Pavel Shilovsky | a0e7318 | 2011-07-19 12:56:37 +0400 | [diff] [blame] | 162 | |
| 163 | int |
| 164 | smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 165 | struct cifs_sb_info *cifs_sb) |
| 166 | { |
| 167 | return smb2_open_op_close(xid, tcon, cifs_sb, name, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 168 | FILE_WRITE_ATTRIBUTES, FILE_CREATE, |
Pavel Shilovsky | a0e7318 | 2011-07-19 12:56:37 +0400 | [diff] [blame] | 169 | CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | smb2_mkdir_setinfo(struct inode *inode, const char *name, |
| 174 | struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon, |
| 175 | const unsigned int xid) |
| 176 | { |
| 177 | FILE_BASIC_INFO data; |
| 178 | struct cifsInodeInfo *cifs_i; |
| 179 | u32 dosattrs; |
| 180 | int tmprc; |
| 181 | |
| 182 | memset(&data, 0, sizeof(data)); |
| 183 | cifs_i = CIFS_I(inode); |
| 184 | dosattrs = cifs_i->cifsAttrs | ATTR_READONLY; |
| 185 | data.Attributes = cpu_to_le32(dosattrs); |
| 186 | tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 187 | FILE_WRITE_ATTRIBUTES, FILE_CREATE, |
Pavel Shilovsky | a0e7318 | 2011-07-19 12:56:37 +0400 | [diff] [blame] | 188 | CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO); |
| 189 | if (tmprc == 0) |
| 190 | cifs_i->cifsAttrs = dosattrs; |
| 191 | } |
Pavel Shilovsky | 1a500f0 | 2012-07-10 16:14:38 +0400 | [diff] [blame] | 192 | |
| 193 | int |
| 194 | smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 195 | struct cifs_sb_info *cifs_sb) |
| 196 | { |
| 197 | return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN, |
Steve French | 897fba1 | 2016-05-12 21:20:36 -0500 | [diff] [blame] | 198 | CREATE_NOT_FILE, |
| 199 | NULL, SMB2_OP_RMDIR); |
Pavel Shilovsky | 1a500f0 | 2012-07-10 16:14:38 +0400 | [diff] [blame] | 200 | } |
Pavel Shilovsky | cbe6f43 | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 201 | |
| 202 | int |
| 203 | smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name, |
| 204 | struct cifs_sb_info *cifs_sb) |
| 205 | { |
| 206 | return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN, |
Pavel Shilovsky | b42bf88 | 2013-08-14 19:25:21 +0400 | [diff] [blame] | 207 | CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT, |
| 208 | NULL, SMB2_OP_DELETE); |
Pavel Shilovsky | cbe6f43 | 2012-09-18 16:20:25 -0700 | [diff] [blame] | 209 | } |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 210 | |
Pavel Shilovsky | 568798c | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 211 | static int |
| 212 | smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon, |
| 213 | const char *from_name, const char *to_name, |
| 214 | struct cifs_sb_info *cifs_sb, __u32 access, int command) |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 215 | { |
| 216 | __le16 *smb2_to_name = NULL; |
| 217 | int rc; |
| 218 | |
| 219 | smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb); |
| 220 | if (smb2_to_name == NULL) { |
| 221 | rc = -ENOMEM; |
| 222 | goto smb2_rename_path; |
| 223 | } |
| 224 | |
Pavel Shilovsky | 568798c | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 225 | rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 226 | FILE_OPEN, 0, smb2_to_name, command); |
Pavel Shilovsky | 35143eb | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 227 | smb2_rename_path: |
| 228 | kfree(smb2_to_name); |
| 229 | return rc; |
| 230 | } |
Pavel Shilovsky | 568798c | 2012-09-18 16:20:31 -0700 | [diff] [blame] | 231 | |
| 232 | int |
| 233 | smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon, |
| 234 | const char *from_name, const char *to_name, |
| 235 | struct cifs_sb_info *cifs_sb) |
| 236 | { |
| 237 | return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, |
| 238 | DELETE, SMB2_OP_RENAME); |
| 239 | } |
| 240 | |
| 241 | int |
| 242 | smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon, |
| 243 | const char *from_name, const char *to_name, |
| 244 | struct cifs_sb_info *cifs_sb) |
| 245 | { |
| 246 | return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb, |
| 247 | FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK); |
| 248 | } |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 249 | |
| 250 | int |
| 251 | smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon, |
| 252 | const char *full_path, __u64 size, |
| 253 | struct cifs_sb_info *cifs_sb, bool set_alloc) |
| 254 | { |
| 255 | __le64 eof = cpu_to_le64(size); |
| 256 | return smb2_open_op_close(xid, tcon, cifs_sb, full_path, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 257 | FILE_WRITE_DATA, FILE_OPEN, 0, &eof, |
Pavel Shilovsky | c839ff2 | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 258 | SMB2_OP_SET_EOF); |
| 259 | } |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 260 | |
| 261 | int |
| 262 | smb2_set_file_info(struct inode *inode, const char *full_path, |
| 263 | FILE_BASIC_INFO *buf, const unsigned int xid) |
| 264 | { |
| 265 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 266 | struct tcon_link *tlink; |
| 267 | int rc; |
| 268 | |
| 269 | tlink = cifs_sb_tlink(cifs_sb); |
| 270 | if (IS_ERR(tlink)) |
| 271 | return PTR_ERR(tlink); |
| 272 | rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path, |
Pavel Shilovsky | ca81983 | 2013-07-05 12:21:26 +0400 | [diff] [blame] | 273 | FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf, |
Pavel Shilovsky | 1feeaac | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 274 | SMB2_OP_SET_INFO); |
| 275 | cifs_put_tlink(tlink); |
| 276 | return rc; |
| 277 | } |