0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/pagemap.h>
0009 #include <linux/vfs.h>
0010 #include <uapi/linux/magic.h>
0011 #include "cifsglob.h"
0012 #include "cifsproto.h"
0013 #include "cifs_debug.h"
0014 #include "cifspdu.h"
0015 #include "cifs_unicode.h"
0016 #include "fs_context.h"
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 static int
0029 send_nt_cancel(struct TCP_Server_Info *server, struct smb_rqst *rqst,
0030 struct mid_q_entry *mid)
0031 {
0032 int rc = 0;
0033 struct smb_hdr *in_buf = (struct smb_hdr *)rqst->rq_iov[0].iov_base;
0034
0035
0036 in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
0037 in_buf->Command = SMB_COM_NT_CANCEL;
0038 in_buf->WordCount = 0;
0039 put_bcc(0, in_buf);
0040
0041 cifs_server_lock(server);
0042 rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
0043 if (rc) {
0044 cifs_server_unlock(server);
0045 return rc;
0046 }
0047
0048
0049
0050
0051
0052
0053 --server->sequence_number;
0054 rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
0055 if (rc < 0)
0056 server->sequence_number--;
0057
0058 cifs_server_unlock(server);
0059
0060 cifs_dbg(FYI, "issued NT_CANCEL for mid %u, rc = %d\n",
0061 get_mid(in_buf), rc);
0062
0063 return rc;
0064 }
0065
0066 static bool
0067 cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
0068 {
0069 return ob1->fid.netfid == ob2->fid.netfid;
0070 }
0071
0072 static unsigned int
0073 cifs_read_data_offset(char *buf)
0074 {
0075 READ_RSP *rsp = (READ_RSP *)buf;
0076 return le16_to_cpu(rsp->DataOffset);
0077 }
0078
0079 static unsigned int
0080 cifs_read_data_length(char *buf, bool in_remaining)
0081 {
0082 READ_RSP *rsp = (READ_RSP *)buf;
0083
0084 WARN_ON(in_remaining);
0085 return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
0086 le16_to_cpu(rsp->DataLength);
0087 }
0088
0089 static struct mid_q_entry *
0090 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
0091 {
0092 struct smb_hdr *buf = (struct smb_hdr *)buffer;
0093 struct mid_q_entry *mid;
0094
0095 spin_lock(&server->mid_lock);
0096 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
0097 if (compare_mid(mid->mid, buf) &&
0098 mid->mid_state == MID_REQUEST_SUBMITTED &&
0099 le16_to_cpu(mid->command) == buf->Command) {
0100 kref_get(&mid->refcount);
0101 spin_unlock(&server->mid_lock);
0102 return mid;
0103 }
0104 }
0105 spin_unlock(&server->mid_lock);
0106 return NULL;
0107 }
0108
0109 static void
0110 cifs_add_credits(struct TCP_Server_Info *server,
0111 const struct cifs_credits *credits, const int optype)
0112 {
0113 spin_lock(&server->req_lock);
0114 server->credits += credits->value;
0115 server->in_flight--;
0116 spin_unlock(&server->req_lock);
0117 wake_up(&server->request_q);
0118 }
0119
0120 static void
0121 cifs_set_credits(struct TCP_Server_Info *server, const int val)
0122 {
0123 spin_lock(&server->req_lock);
0124 server->credits = val;
0125 server->oplocks = val > 1 ? enable_oplocks : false;
0126 spin_unlock(&server->req_lock);
0127 }
0128
0129 static int *
0130 cifs_get_credits_field(struct TCP_Server_Info *server, const int optype)
0131 {
0132 return &server->credits;
0133 }
0134
0135 static unsigned int
0136 cifs_get_credits(struct mid_q_entry *mid)
0137 {
0138 return 1;
0139 }
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162 static __u64
0163 cifs_get_next_mid(struct TCP_Server_Info *server)
0164 {
0165 __u64 mid = 0;
0166 __u16 last_mid, cur_mid;
0167 bool collision, reconnect = false;
0168
0169 spin_lock(&server->mid_lock);
0170
0171
0172 cur_mid = (__u16)((server->CurrentMid) & 0xffff);
0173
0174 last_mid = cur_mid;
0175 cur_mid++;
0176
0177 if (cur_mid == 0xffff)
0178 cur_mid++;
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 while (cur_mid != last_mid) {
0190 struct mid_q_entry *mid_entry;
0191 unsigned int num_mids;
0192
0193 collision = false;
0194 if (cur_mid == 0)
0195 cur_mid++;
0196
0197 num_mids = 0;
0198 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
0199 ++num_mids;
0200 if (mid_entry->mid == cur_mid &&
0201 mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
0202
0203 collision = true;
0204 break;
0205 }
0206 }
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 if (num_mids > 32768)
0219 reconnect = true;
0220
0221 if (!collision) {
0222 mid = (__u64)cur_mid;
0223 server->CurrentMid = mid;
0224 break;
0225 }
0226 cur_mid++;
0227 }
0228 spin_unlock(&server->mid_lock);
0229
0230 if (reconnect) {
0231 cifs_signal_cifsd_for_reconnect(server, false);
0232 }
0233
0234 return mid;
0235 }
0236
0237
0238
0239
0240
0241
0242
0243 static int
0244 check2ndT2(char *buf)
0245 {
0246 struct smb_hdr *pSMB = (struct smb_hdr *)buf;
0247 struct smb_t2_rsp *pSMBt;
0248 int remaining;
0249 __u16 total_data_size, data_in_this_rsp;
0250
0251 if (pSMB->Command != SMB_COM_TRANSACTION2)
0252 return 0;
0253
0254
0255
0256 if (pSMB->WordCount != 10) {
0257 cifs_dbg(FYI, "Invalid transact2 word count\n");
0258 return -EINVAL;
0259 }
0260
0261 pSMBt = (struct smb_t2_rsp *)pSMB;
0262
0263 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
0264 data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
0265
0266 if (total_data_size == data_in_this_rsp)
0267 return 0;
0268 else if (total_data_size < data_in_this_rsp) {
0269 cifs_dbg(FYI, "total data %d smaller than data in frame %d\n",
0270 total_data_size, data_in_this_rsp);
0271 return -EINVAL;
0272 }
0273
0274 remaining = total_data_size - data_in_this_rsp;
0275
0276 cifs_dbg(FYI, "missing %d bytes from transact2, check next response\n",
0277 remaining);
0278 if (total_data_size > CIFSMaxBufSize) {
0279 cifs_dbg(VFS, "TotalDataSize %d is over maximum buffer %d\n",
0280 total_data_size, CIFSMaxBufSize);
0281 return -EINVAL;
0282 }
0283 return remaining;
0284 }
0285
0286 static int
0287 coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
0288 {
0289 struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
0290 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)target_hdr;
0291 char *data_area_of_tgt;
0292 char *data_area_of_src;
0293 int remaining;
0294 unsigned int byte_count, total_in_tgt;
0295 __u16 tgt_total_cnt, src_total_cnt, total_in_src;
0296
0297 src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
0298 tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
0299
0300 if (tgt_total_cnt != src_total_cnt)
0301 cifs_dbg(FYI, "total data count of primary and secondary t2 differ source=%hu target=%hu\n",
0302 src_total_cnt, tgt_total_cnt);
0303
0304 total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
0305
0306 remaining = tgt_total_cnt - total_in_tgt;
0307
0308 if (remaining < 0) {
0309 cifs_dbg(FYI, "Server sent too much data. tgt_total_cnt=%hu total_in_tgt=%u\n",
0310 tgt_total_cnt, total_in_tgt);
0311 return -EPROTO;
0312 }
0313
0314 if (remaining == 0) {
0315
0316 cifs_dbg(FYI, "no more data remains\n");
0317 return 0;
0318 }
0319
0320 total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
0321 if (remaining < total_in_src)
0322 cifs_dbg(FYI, "transact2 2nd response contains too much data\n");
0323
0324
0325 data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
0326 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
0327
0328
0329 data_area_of_src = (char *)&pSMBs->hdr.Protocol +
0330 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
0331
0332 data_area_of_tgt += total_in_tgt;
0333
0334 total_in_tgt += total_in_src;
0335
0336 if (total_in_tgt > USHRT_MAX) {
0337 cifs_dbg(FYI, "coalesced DataCount too large (%u)\n",
0338 total_in_tgt);
0339 return -EPROTO;
0340 }
0341 put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
0342
0343
0344 byte_count = get_bcc(target_hdr);
0345 byte_count += total_in_src;
0346
0347 if (byte_count > USHRT_MAX) {
0348 cifs_dbg(FYI, "coalesced BCC too large (%u)\n", byte_count);
0349 return -EPROTO;
0350 }
0351 put_bcc(byte_count, target_hdr);
0352
0353 byte_count = be32_to_cpu(target_hdr->smb_buf_length);
0354 byte_count += total_in_src;
0355
0356 if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
0357 cifs_dbg(FYI, "coalesced BCC exceeds buffer size (%u)\n",
0358 byte_count);
0359 return -ENOBUFS;
0360 }
0361 target_hdr->smb_buf_length = cpu_to_be32(byte_count);
0362
0363
0364 memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
0365
0366 if (remaining != total_in_src) {
0367
0368 cifs_dbg(FYI, "waiting for more secondary responses\n");
0369 return 1;
0370 }
0371
0372
0373 cifs_dbg(FYI, "found the last secondary response\n");
0374 return 0;
0375 }
0376
0377 static void
0378 cifs_downgrade_oplock(struct TCP_Server_Info *server,
0379 struct cifsInodeInfo *cinode, __u32 oplock,
0380 unsigned int epoch, bool *purge_cache)
0381 {
0382 cifs_set_oplock_level(cinode, oplock);
0383 }
0384
0385 static bool
0386 cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
0387 char *buf, int malformed)
0388 {
0389 if (malformed)
0390 return false;
0391 if (check2ndT2(buf) <= 0)
0392 return false;
0393 mid->multiRsp = true;
0394 if (mid->resp_buf) {
0395
0396 malformed = coalesce_t2(buf, mid->resp_buf);
0397 if (malformed > 0)
0398 return true;
0399
0400 mid->multiEnd = true;
0401 dequeue_mid(mid, malformed);
0402 return true;
0403 }
0404 if (!server->large_buf) {
0405
0406 cifs_dbg(VFS, "1st trans2 resp needs bigbuf\n");
0407 } else {
0408
0409 mid->resp_buf = buf;
0410 mid->large_buf = true;
0411 server->bigbuf = NULL;
0412 }
0413 return true;
0414 }
0415
0416 static bool
0417 cifs_need_neg(struct TCP_Server_Info *server)
0418 {
0419 return server->maxBuf == 0;
0420 }
0421
0422 static int
0423 cifs_negotiate(const unsigned int xid,
0424 struct cifs_ses *ses,
0425 struct TCP_Server_Info *server)
0426 {
0427 int rc;
0428 rc = CIFSSMBNegotiate(xid, ses, server);
0429 if (rc == -EAGAIN) {
0430
0431 set_credits(server, 1);
0432 rc = CIFSSMBNegotiate(xid, ses, server);
0433 if (rc == -EAGAIN)
0434 rc = -EHOSTDOWN;
0435 }
0436 return rc;
0437 }
0438
0439 static unsigned int
0440 cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
0441 {
0442 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
0443 struct TCP_Server_Info *server = tcon->ses->server;
0444 unsigned int wsize;
0445
0446
0447 if (ctx->wsize)
0448 wsize = ctx->wsize;
0449 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
0450 wsize = CIFS_DEFAULT_IOSIZE;
0451 else
0452 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
0453
0454
0455 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
0456 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
0457
0458
0459
0460
0461
0462
0463 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
0464 (!(server->capabilities & CAP_UNIX) && server->sign))
0465 wsize = min_t(unsigned int, wsize,
0466 server->maxBuf - sizeof(WRITE_REQ) + 4);
0467
0468
0469 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
0470
0471 return wsize;
0472 }
0473
0474 static unsigned int
0475 cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
0476 {
0477 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
0478 struct TCP_Server_Info *server = tcon->ses->server;
0479 unsigned int rsize, defsize;
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
0494 defsize = CIFS_DEFAULT_IOSIZE;
0495 else if (server->capabilities & CAP_LARGE_READ_X)
0496 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
0497 else
0498 defsize = server->maxBuf - sizeof(READ_RSP);
0499
0500 rsize = ctx->rsize ? ctx->rsize : defsize;
0501
0502
0503
0504
0505
0506 if (!(server->capabilities & CAP_LARGE_READ_X))
0507 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
0508
0509
0510 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
0511
0512 return rsize;
0513 }
0514
0515 static void
0516 cifs_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
0517 struct cifs_sb_info *cifs_sb)
0518 {
0519 CIFSSMBQFSDeviceInfo(xid, tcon);
0520 CIFSSMBQFSAttributeInfo(xid, tcon);
0521 }
0522
0523 static int
0524 cifs_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
0525 struct cifs_sb_info *cifs_sb, const char *full_path)
0526 {
0527 int rc;
0528 FILE_ALL_INFO *file_info;
0529
0530 file_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
0531 if (file_info == NULL)
0532 return -ENOMEM;
0533
0534 rc = CIFSSMBQPathInfo(xid, tcon, full_path, file_info,
0535 0 , cifs_sb->local_nls,
0536 cifs_remap(cifs_sb));
0537
0538 if (rc == -EOPNOTSUPP || rc == -EINVAL)
0539 rc = SMBQueryInformation(xid, tcon, full_path, file_info,
0540 cifs_sb->local_nls, cifs_remap(cifs_sb));
0541 kfree(file_info);
0542 return rc;
0543 }
0544
0545 static int
0546 cifs_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
0547 struct cifs_sb_info *cifs_sb, const char *full_path,
0548 FILE_ALL_INFO *data, bool *adjustTZ, bool *symlink)
0549 {
0550 int rc;
0551
0552 *symlink = false;
0553
0554
0555 rc = CIFSSMBQPathInfo(xid, tcon, full_path, data, 0 ,
0556 cifs_sb->local_nls, cifs_remap(cifs_sb));
0557
0558
0559
0560
0561
0562 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
0563 rc = SMBQueryInformation(xid, tcon, full_path, data,
0564 cifs_sb->local_nls,
0565 cifs_remap(cifs_sb));
0566 *adjustTZ = true;
0567 }
0568
0569 if (!rc && (le32_to_cpu(data->Attributes) & ATTR_REPARSE)) {
0570 int tmprc;
0571 int oplock = 0;
0572 struct cifs_fid fid;
0573 struct cifs_open_parms oparms;
0574
0575 oparms.tcon = tcon;
0576 oparms.cifs_sb = cifs_sb;
0577 oparms.desired_access = FILE_READ_ATTRIBUTES;
0578 oparms.create_options = cifs_create_options(cifs_sb, 0);
0579 oparms.disposition = FILE_OPEN;
0580 oparms.path = full_path;
0581 oparms.fid = &fid;
0582 oparms.reconnect = false;
0583
0584
0585 tmprc = CIFS_open(xid, &oparms, &oplock, NULL);
0586 if (tmprc == -EOPNOTSUPP)
0587 *symlink = true;
0588 else if (tmprc == 0)
0589 CIFSSMBClose(xid, tcon, fid.netfid);
0590 }
0591
0592 return rc;
0593 }
0594
0595 static int
0596 cifs_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
0597 struct cifs_sb_info *cifs_sb, const char *full_path,
0598 u64 *uniqueid, FILE_ALL_INFO *data)
0599 {
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611 return CIFSGetSrvInodeNumber(xid, tcon, full_path, uniqueid,
0612 cifs_sb->local_nls,
0613 cifs_remap(cifs_sb));
0614 }
0615
0616 static int
0617 cifs_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
0618 struct cifs_fid *fid, FILE_ALL_INFO *data)
0619 {
0620 return CIFSSMBQFileInfo(xid, tcon, fid->netfid, data);
0621 }
0622
0623 static void
0624 cifs_clear_stats(struct cifs_tcon *tcon)
0625 {
0626 atomic_set(&tcon->stats.cifs_stats.num_writes, 0);
0627 atomic_set(&tcon->stats.cifs_stats.num_reads, 0);
0628 atomic_set(&tcon->stats.cifs_stats.num_flushes, 0);
0629 atomic_set(&tcon->stats.cifs_stats.num_oplock_brks, 0);
0630 atomic_set(&tcon->stats.cifs_stats.num_opens, 0);
0631 atomic_set(&tcon->stats.cifs_stats.num_posixopens, 0);
0632 atomic_set(&tcon->stats.cifs_stats.num_posixmkdirs, 0);
0633 atomic_set(&tcon->stats.cifs_stats.num_closes, 0);
0634 atomic_set(&tcon->stats.cifs_stats.num_deletes, 0);
0635 atomic_set(&tcon->stats.cifs_stats.num_mkdirs, 0);
0636 atomic_set(&tcon->stats.cifs_stats.num_rmdirs, 0);
0637 atomic_set(&tcon->stats.cifs_stats.num_renames, 0);
0638 atomic_set(&tcon->stats.cifs_stats.num_t2renames, 0);
0639 atomic_set(&tcon->stats.cifs_stats.num_ffirst, 0);
0640 atomic_set(&tcon->stats.cifs_stats.num_fnext, 0);
0641 atomic_set(&tcon->stats.cifs_stats.num_fclose, 0);
0642 atomic_set(&tcon->stats.cifs_stats.num_hardlinks, 0);
0643 atomic_set(&tcon->stats.cifs_stats.num_symlinks, 0);
0644 atomic_set(&tcon->stats.cifs_stats.num_locks, 0);
0645 atomic_set(&tcon->stats.cifs_stats.num_acl_get, 0);
0646 atomic_set(&tcon->stats.cifs_stats.num_acl_set, 0);
0647 }
0648
0649 static void
0650 cifs_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
0651 {
0652 seq_printf(m, " Oplocks breaks: %d",
0653 atomic_read(&tcon->stats.cifs_stats.num_oplock_brks));
0654 seq_printf(m, "\nReads: %d Bytes: %llu",
0655 atomic_read(&tcon->stats.cifs_stats.num_reads),
0656 (long long)(tcon->bytes_read));
0657 seq_printf(m, "\nWrites: %d Bytes: %llu",
0658 atomic_read(&tcon->stats.cifs_stats.num_writes),
0659 (long long)(tcon->bytes_written));
0660 seq_printf(m, "\nFlushes: %d",
0661 atomic_read(&tcon->stats.cifs_stats.num_flushes));
0662 seq_printf(m, "\nLocks: %d HardLinks: %d Symlinks: %d",
0663 atomic_read(&tcon->stats.cifs_stats.num_locks),
0664 atomic_read(&tcon->stats.cifs_stats.num_hardlinks),
0665 atomic_read(&tcon->stats.cifs_stats.num_symlinks));
0666 seq_printf(m, "\nOpens: %d Closes: %d Deletes: %d",
0667 atomic_read(&tcon->stats.cifs_stats.num_opens),
0668 atomic_read(&tcon->stats.cifs_stats.num_closes),
0669 atomic_read(&tcon->stats.cifs_stats.num_deletes));
0670 seq_printf(m, "\nPosix Opens: %d Posix Mkdirs: %d",
0671 atomic_read(&tcon->stats.cifs_stats.num_posixopens),
0672 atomic_read(&tcon->stats.cifs_stats.num_posixmkdirs));
0673 seq_printf(m, "\nMkdirs: %d Rmdirs: %d",
0674 atomic_read(&tcon->stats.cifs_stats.num_mkdirs),
0675 atomic_read(&tcon->stats.cifs_stats.num_rmdirs));
0676 seq_printf(m, "\nRenames: %d T2 Renames %d",
0677 atomic_read(&tcon->stats.cifs_stats.num_renames),
0678 atomic_read(&tcon->stats.cifs_stats.num_t2renames));
0679 seq_printf(m, "\nFindFirst: %d FNext %d FClose %d",
0680 atomic_read(&tcon->stats.cifs_stats.num_ffirst),
0681 atomic_read(&tcon->stats.cifs_stats.num_fnext),
0682 atomic_read(&tcon->stats.cifs_stats.num_fclose));
0683 }
0684
0685 static void
0686 cifs_mkdir_setinfo(struct inode *inode, const char *full_path,
0687 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
0688 const unsigned int xid)
0689 {
0690 FILE_BASIC_INFO info;
0691 struct cifsInodeInfo *cifsInode;
0692 u32 dosattrs;
0693 int rc;
0694
0695 memset(&info, 0, sizeof(info));
0696 cifsInode = CIFS_I(inode);
0697 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
0698 info.Attributes = cpu_to_le32(dosattrs);
0699 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, &info, cifs_sb->local_nls,
0700 cifs_sb);
0701 if (rc == 0)
0702 cifsInode->cifsAttrs = dosattrs;
0703 }
0704
0705 static int
0706 cifs_open_file(const unsigned int xid, struct cifs_open_parms *oparms,
0707 __u32 *oplock, FILE_ALL_INFO *buf)
0708 {
0709 if (!(oparms->tcon->ses->capabilities & CAP_NT_SMBS))
0710 return SMBLegacyOpen(xid, oparms->tcon, oparms->path,
0711 oparms->disposition,
0712 oparms->desired_access,
0713 oparms->create_options,
0714 &oparms->fid->netfid, oplock, buf,
0715 oparms->cifs_sb->local_nls,
0716 cifs_remap(oparms->cifs_sb));
0717 return CIFS_open(xid, oparms, oplock, buf);
0718 }
0719
0720 static void
0721 cifs_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
0722 {
0723 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
0724 cfile->fid.netfid = fid->netfid;
0725 cifs_set_oplock_level(cinode, oplock);
0726 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
0727 }
0728
0729 static void
0730 cifs_close_file(const unsigned int xid, struct cifs_tcon *tcon,
0731 struct cifs_fid *fid)
0732 {
0733 CIFSSMBClose(xid, tcon, fid->netfid);
0734 }
0735
0736 static int
0737 cifs_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
0738 struct cifs_fid *fid)
0739 {
0740 return CIFSSMBFlush(xid, tcon, fid->netfid);
0741 }
0742
0743 static int
0744 cifs_sync_read(const unsigned int xid, struct cifs_fid *pfid,
0745 struct cifs_io_parms *parms, unsigned int *bytes_read,
0746 char **buf, int *buf_type)
0747 {
0748 parms->netfid = pfid->netfid;
0749 return CIFSSMBRead(xid, parms, bytes_read, buf, buf_type);
0750 }
0751
0752 static int
0753 cifs_sync_write(const unsigned int xid, struct cifs_fid *pfid,
0754 struct cifs_io_parms *parms, unsigned int *written,
0755 struct kvec *iov, unsigned long nr_segs)
0756 {
0757
0758 parms->netfid = pfid->netfid;
0759 return CIFSSMBWrite2(xid, parms, written, iov, nr_segs);
0760 }
0761
0762 static int
0763 smb_set_file_info(struct inode *inode, const char *full_path,
0764 FILE_BASIC_INFO *buf, const unsigned int xid)
0765 {
0766 int oplock = 0;
0767 int rc;
0768 __u32 netpid;
0769 struct cifs_fid fid;
0770 struct cifs_open_parms oparms;
0771 struct cifsFileInfo *open_file;
0772 struct cifsInodeInfo *cinode = CIFS_I(inode);
0773 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
0774 struct tcon_link *tlink = NULL;
0775 struct cifs_tcon *tcon;
0776
0777
0778 open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
0779 if (open_file) {
0780 fid.netfid = open_file->fid.netfid;
0781 netpid = open_file->pid;
0782 tcon = tlink_tcon(open_file->tlink);
0783 goto set_via_filehandle;
0784 }
0785
0786 tlink = cifs_sb_tlink(cifs_sb);
0787 if (IS_ERR(tlink)) {
0788 rc = PTR_ERR(tlink);
0789 tlink = NULL;
0790 goto out;
0791 }
0792 tcon = tlink_tcon(tlink);
0793
0794 rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
0795 cifs_sb);
0796 if (rc == 0) {
0797 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
0798 goto out;
0799 } else if (rc != -EOPNOTSUPP && rc != -EINVAL) {
0800 goto out;
0801 }
0802
0803 oparms.tcon = tcon;
0804 oparms.cifs_sb = cifs_sb;
0805 oparms.desired_access = SYNCHRONIZE | FILE_WRITE_ATTRIBUTES;
0806 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
0807 oparms.disposition = FILE_OPEN;
0808 oparms.path = full_path;
0809 oparms.fid = &fid;
0810 oparms.reconnect = false;
0811
0812 cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
0813 rc = CIFS_open(xid, &oparms, &oplock, NULL);
0814 if (rc != 0) {
0815 if (rc == -EIO)
0816 rc = -EINVAL;
0817 goto out;
0818 }
0819
0820 netpid = current->tgid;
0821
0822 set_via_filehandle:
0823 rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
0824 if (!rc)
0825 cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
0826
0827 if (open_file == NULL)
0828 CIFSSMBClose(xid, tcon, fid.netfid);
0829 else
0830 cifsFileInfo_put(open_file);
0831 out:
0832 if (tlink != NULL)
0833 cifs_put_tlink(tlink);
0834 return rc;
0835 }
0836
0837 static int
0838 cifs_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
0839 struct cifsFileInfo *cfile)
0840 {
0841 return CIFSSMB_set_compression(xid, tcon, cfile->fid.netfid);
0842 }
0843
0844 static int
0845 cifs_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
0846 const char *path, struct cifs_sb_info *cifs_sb,
0847 struct cifs_fid *fid, __u16 search_flags,
0848 struct cifs_search_info *srch_inf)
0849 {
0850 int rc;
0851
0852 rc = CIFSFindFirst(xid, tcon, path, cifs_sb,
0853 &fid->netfid, search_flags, srch_inf, true);
0854 if (rc)
0855 cifs_dbg(FYI, "find first failed=%d\n", rc);
0856 return rc;
0857 }
0858
0859 static int
0860 cifs_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
0861 struct cifs_fid *fid, __u16 search_flags,
0862 struct cifs_search_info *srch_inf)
0863 {
0864 return CIFSFindNext(xid, tcon, fid->netfid, search_flags, srch_inf);
0865 }
0866
0867 static int
0868 cifs_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
0869 struct cifs_fid *fid)
0870 {
0871 return CIFSFindClose(xid, tcon, fid->netfid);
0872 }
0873
0874 static int
0875 cifs_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
0876 struct cifsInodeInfo *cinode)
0877 {
0878 return CIFSSMBLock(0, tcon, fid->netfid, current->tgid, 0, 0, 0, 0,
0879 LOCKING_ANDX_OPLOCK_RELEASE, false,
0880 CIFS_CACHE_READ(cinode) ? 1 : 0);
0881 }
0882
0883 static int
0884 cifs_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
0885 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
0886 {
0887 int rc = -EOPNOTSUPP;
0888
0889 buf->f_type = CIFS_SUPER_MAGIC;
0890
0891
0892
0893
0894 if ((tcon->ses->capabilities & CAP_UNIX) &&
0895 (CIFS_POSIX_EXTENSIONS & le64_to_cpu(tcon->fsUnixInfo.Capability)))
0896 rc = CIFSSMBQFSPosixInfo(xid, tcon, buf);
0897
0898
0899
0900
0901
0902 if (rc && (tcon->ses->capabilities & CAP_NT_SMBS))
0903 rc = CIFSSMBQFSInfo(xid, tcon, buf);
0904
0905
0906
0907
0908
0909
0910 if (rc)
0911 rc = SMBOldQFSInfo(xid, tcon, buf);
0912 return rc;
0913 }
0914
0915 static int
0916 cifs_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
0917 __u64 length, __u32 type, int lock, int unlock, bool wait)
0918 {
0919 return CIFSSMBLock(xid, tlink_tcon(cfile->tlink), cfile->fid.netfid,
0920 current->tgid, length, offset, unlock, lock,
0921 (__u8)type, wait, 0);
0922 }
0923
0924 static int
0925 cifs_unix_dfs_readlink(const unsigned int xid, struct cifs_tcon *tcon,
0926 const unsigned char *searchName, char **symlinkinfo,
0927 const struct nls_table *nls_codepage)
0928 {
0929 #ifdef CONFIG_CIFS_DFS_UPCALL
0930 int rc;
0931 struct dfs_info3_param referral = {0};
0932
0933 rc = get_dfs_path(xid, tcon->ses, searchName, nls_codepage, &referral,
0934 0);
0935
0936 if (!rc) {
0937 *symlinkinfo = kstrdup(referral.node_name, GFP_KERNEL);
0938 free_dfs_info_param(&referral);
0939 if (!*symlinkinfo)
0940 rc = -ENOMEM;
0941 }
0942 return rc;
0943 #else
0944 return -EREMOTE;
0945 #endif
0946 }
0947
0948 static int
0949 cifs_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
0950 struct cifs_sb_info *cifs_sb, const char *full_path,
0951 char **target_path, bool is_reparse_point)
0952 {
0953 int rc;
0954 int oplock = 0;
0955 struct cifs_fid fid;
0956 struct cifs_open_parms oparms;
0957
0958 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
0959
0960 if (is_reparse_point) {
0961 cifs_dbg(VFS, "reparse points not handled for SMB1 symlinks\n");
0962 return -EOPNOTSUPP;
0963 }
0964
0965
0966 if (cap_unix(tcon->ses)) {
0967 rc = CIFSSMBUnixQuerySymLink(xid, tcon, full_path, target_path,
0968 cifs_sb->local_nls,
0969 cifs_remap(cifs_sb));
0970 if (rc == -EREMOTE)
0971 rc = cifs_unix_dfs_readlink(xid, tcon, full_path,
0972 target_path,
0973 cifs_sb->local_nls);
0974
0975 goto out;
0976 }
0977
0978 oparms.tcon = tcon;
0979 oparms.cifs_sb = cifs_sb;
0980 oparms.desired_access = FILE_READ_ATTRIBUTES;
0981 oparms.create_options = cifs_create_options(cifs_sb,
0982 OPEN_REPARSE_POINT);
0983 oparms.disposition = FILE_OPEN;
0984 oparms.path = full_path;
0985 oparms.fid = &fid;
0986 oparms.reconnect = false;
0987
0988 rc = CIFS_open(xid, &oparms, &oplock, NULL);
0989 if (rc)
0990 goto out;
0991
0992 rc = CIFSSMBQuerySymLink(xid, tcon, fid.netfid, target_path,
0993 cifs_sb->local_nls);
0994 if (rc)
0995 goto out_close;
0996
0997 convert_delimiter(*target_path, '/');
0998 out_close:
0999 CIFSSMBClose(xid, tcon, fid.netfid);
1000 out:
1001 if (!rc)
1002 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1003 return rc;
1004 }
1005
1006 static bool
1007 cifs_is_read_op(__u32 oplock)
1008 {
1009 return oplock == OPLOCK_READ;
1010 }
1011
1012 static unsigned int
1013 cifs_wp_retry_size(struct inode *inode)
1014 {
1015 return CIFS_SB(inode->i_sb)->ctx->wsize;
1016 }
1017
1018 static bool
1019 cifs_dir_needs_close(struct cifsFileInfo *cfile)
1020 {
1021 return !cfile->srch_inf.endOfSearch && !cfile->invalidHandle;
1022 }
1023
1024 static bool
1025 cifs_can_echo(struct TCP_Server_Info *server)
1026 {
1027 if (server->tcpStatus == CifsGood)
1028 return true;
1029
1030 return false;
1031 }
1032
1033 static int
1034 cifs_make_node(unsigned int xid, struct inode *inode,
1035 struct dentry *dentry, struct cifs_tcon *tcon,
1036 const char *full_path, umode_t mode, dev_t dev)
1037 {
1038 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1039 struct inode *newinode = NULL;
1040 int rc = -EPERM;
1041 FILE_ALL_INFO *buf = NULL;
1042 struct cifs_io_parms io_parms;
1043 __u32 oplock = 0;
1044 struct cifs_fid fid;
1045 struct cifs_open_parms oparms;
1046 unsigned int bytes_written;
1047 struct win_dev *pdev;
1048 struct kvec iov[2];
1049
1050 if (tcon->unix_ext) {
1051
1052
1053
1054
1055 struct cifs_unix_set_info_args args = {
1056 .mode = mode & ~current_umask(),
1057 .ctime = NO_CHANGE_64,
1058 .atime = NO_CHANGE_64,
1059 .mtime = NO_CHANGE_64,
1060 .device = dev,
1061 };
1062 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1063 args.uid = current_fsuid();
1064 args.gid = current_fsgid();
1065 } else {
1066 args.uid = INVALID_UID;
1067 args.gid = INVALID_GID;
1068 }
1069 rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1070 cifs_sb->local_nls,
1071 cifs_remap(cifs_sb));
1072 if (rc)
1073 goto out;
1074
1075 rc = cifs_get_inode_info_unix(&newinode, full_path,
1076 inode->i_sb, xid);
1077
1078 if (rc == 0)
1079 d_instantiate(dentry, newinode);
1080 goto out;
1081 }
1082
1083
1084
1085
1086
1087 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
1088 goto out;
1089
1090 if (!S_ISCHR(mode) && !S_ISBLK(mode))
1091 goto out;
1092
1093 cifs_dbg(FYI, "sfu compat create special file\n");
1094
1095 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
1096 if (buf == NULL) {
1097 rc = -ENOMEM;
1098 goto out;
1099 }
1100
1101 oparms.tcon = tcon;
1102 oparms.cifs_sb = cifs_sb;
1103 oparms.desired_access = GENERIC_WRITE;
1104 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
1105 CREATE_OPTION_SPECIAL);
1106 oparms.disposition = FILE_CREATE;
1107 oparms.path = full_path;
1108 oparms.fid = &fid;
1109 oparms.reconnect = false;
1110
1111 if (tcon->ses->server->oplocks)
1112 oplock = REQ_OPLOCK;
1113 else
1114 oplock = 0;
1115 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
1116 if (rc)
1117 goto out;
1118
1119
1120
1121
1122
1123
1124 pdev = (struct win_dev *)buf;
1125 io_parms.pid = current->tgid;
1126 io_parms.tcon = tcon;
1127 io_parms.offset = 0;
1128 io_parms.length = sizeof(struct win_dev);
1129 iov[1].iov_base = buf;
1130 iov[1].iov_len = sizeof(struct win_dev);
1131 if (S_ISCHR(mode)) {
1132 memcpy(pdev->type, "IntxCHR", 8);
1133 pdev->major = cpu_to_le64(MAJOR(dev));
1134 pdev->minor = cpu_to_le64(MINOR(dev));
1135 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1136 &bytes_written, iov, 1);
1137 } else if (S_ISBLK(mode)) {
1138 memcpy(pdev->type, "IntxBLK", 8);
1139 pdev->major = cpu_to_le64(MAJOR(dev));
1140 pdev->minor = cpu_to_le64(MINOR(dev));
1141 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
1142 &bytes_written, iov, 1);
1143 }
1144 tcon->ses->server->ops->close(xid, tcon, &fid);
1145 d_drop(dentry);
1146
1147
1148 out:
1149 kfree(buf);
1150 return rc;
1151 }
1152
1153
1154
1155 struct smb_version_operations smb1_operations = {
1156 .send_cancel = send_nt_cancel,
1157 .compare_fids = cifs_compare_fids,
1158 .setup_request = cifs_setup_request,
1159 .setup_async_request = cifs_setup_async_request,
1160 .check_receive = cifs_check_receive,
1161 .add_credits = cifs_add_credits,
1162 .set_credits = cifs_set_credits,
1163 .get_credits_field = cifs_get_credits_field,
1164 .get_credits = cifs_get_credits,
1165 .wait_mtu_credits = cifs_wait_mtu_credits,
1166 .get_next_mid = cifs_get_next_mid,
1167 .read_data_offset = cifs_read_data_offset,
1168 .read_data_length = cifs_read_data_length,
1169 .map_error = map_smb_to_linux_error,
1170 .find_mid = cifs_find_mid,
1171 .check_message = checkSMB,
1172 .dump_detail = cifs_dump_detail,
1173 .clear_stats = cifs_clear_stats,
1174 .print_stats = cifs_print_stats,
1175 .is_oplock_break = is_valid_oplock_break,
1176 .downgrade_oplock = cifs_downgrade_oplock,
1177 .check_trans2 = cifs_check_trans2,
1178 .need_neg = cifs_need_neg,
1179 .negotiate = cifs_negotiate,
1180 .negotiate_wsize = cifs_negotiate_wsize,
1181 .negotiate_rsize = cifs_negotiate_rsize,
1182 .sess_setup = CIFS_SessSetup,
1183 .logoff = CIFSSMBLogoff,
1184 .tree_connect = CIFSTCon,
1185 .tree_disconnect = CIFSSMBTDis,
1186 .get_dfs_refer = CIFSGetDFSRefer,
1187 .qfs_tcon = cifs_qfs_tcon,
1188 .is_path_accessible = cifs_is_path_accessible,
1189 .can_echo = cifs_can_echo,
1190 .query_path_info = cifs_query_path_info,
1191 .query_file_info = cifs_query_file_info,
1192 .get_srv_inum = cifs_get_srv_inum,
1193 .set_path_size = CIFSSMBSetEOF,
1194 .set_file_size = CIFSSMBSetFileSize,
1195 .set_file_info = smb_set_file_info,
1196 .set_compression = cifs_set_compression,
1197 .echo = CIFSSMBEcho,
1198 .mkdir = CIFSSMBMkDir,
1199 .mkdir_setinfo = cifs_mkdir_setinfo,
1200 .rmdir = CIFSSMBRmDir,
1201 .unlink = CIFSSMBDelFile,
1202 .rename_pending_delete = cifs_rename_pending_delete,
1203 .rename = CIFSSMBRename,
1204 .create_hardlink = CIFSCreateHardLink,
1205 .query_symlink = cifs_query_symlink,
1206 .open = cifs_open_file,
1207 .set_fid = cifs_set_fid,
1208 .close = cifs_close_file,
1209 .flush = cifs_flush_file,
1210 .async_readv = cifs_async_readv,
1211 .async_writev = cifs_async_writev,
1212 .sync_read = cifs_sync_read,
1213 .sync_write = cifs_sync_write,
1214 .query_dir_first = cifs_query_dir_first,
1215 .query_dir_next = cifs_query_dir_next,
1216 .close_dir = cifs_close_dir,
1217 .calc_smb_size = smbCalcSize,
1218 .oplock_response = cifs_oplock_response,
1219 .queryfs = cifs_queryfs,
1220 .mand_lock = cifs_mand_lock,
1221 .mand_unlock_range = cifs_unlock_range,
1222 .push_mand_locks = cifs_push_mandatory_locks,
1223 .query_mf_symlink = cifs_query_mf_symlink,
1224 .create_mf_symlink = cifs_create_mf_symlink,
1225 .is_read_op = cifs_is_read_op,
1226 .wp_retry_size = cifs_wp_retry_size,
1227 .dir_needs_close = cifs_dir_needs_close,
1228 .select_sectype = cifs_select_sectype,
1229 #ifdef CONFIG_CIFS_XATTR
1230 .query_all_EAs = CIFSSMBQAllEAs,
1231 .set_EA = CIFSSMBSetEA,
1232 #endif
1233 .get_acl = get_cifs_acl,
1234 .get_acl_by_fid = get_cifs_acl_by_fid,
1235 .set_acl = set_cifs_acl,
1236 .make_node = cifs_make_node,
1237 };
1238
1239 struct smb_version_values smb1_values = {
1240 .version_string = SMB1_VERSION_STRING,
1241 .protocol_id = SMB10_PROT_ID,
1242 .large_lock_type = LOCKING_ANDX_LARGE_FILES,
1243 .exclusive_lock_type = 0,
1244 .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
1245 .unlock_lock_type = 0,
1246 .header_preamble_size = 4,
1247 .header_size = sizeof(struct smb_hdr),
1248 .max_header_size = MAX_CIFS_HDR_SIZE,
1249 .read_rsp_size = sizeof(READ_RSP),
1250 .lock_cmd = cpu_to_le16(SMB_COM_LOCKING_ANDX),
1251 .cap_unix = CAP_UNIX,
1252 .cap_nt_find = CAP_NT_SMBS | CAP_NT_FIND,
1253 .cap_large_files = CAP_LARGE_FILES,
1254 .signing_enabled = SECMODE_SIGN_ENABLED,
1255 .signing_required = SECMODE_SIGN_REQUIRED,
1256 };