forked from namjaejeon/ksmbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfs.c
1807 lines (1583 loc) · 41 KB
/
vfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org>
* Copyright (C) 2018 Samsung Electronics Co., Ltd.
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/backing-dev.h>
#include <linux/writeback.h>
#include <linux/version.h>
#include <linux/xattr.h>
#include <linux/falloc.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include "glob.h"
#include "oplock.h"
#include "transport_tcp.h"
#include "buffer_pool.h"
#include "vfs.h"
#include "fh.h"
#include "smb_common.h"
#include "mgmt/share_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/user_session.h"
/**
* cifsd_vfs_create() - vfs helper for smb create file
* @name: file name
* @mode: file create mode
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_create(const char *name, umode_t mode)
{
struct path path;
struct dentry *dentry;
int err;
dentry = kern_path_create(AT_FDCWD, name, &path, 0);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
cifsd_err("path create failed for %s, err %d\n", name, err);
return err;
}
mode |= S_IFREG;
err = vfs_create(path.dentry->d_inode, dentry, mode, true);
if (err)
cifsd_err("File(%s): creation failed (err:%d)\n", name, err);
done_path_create(&path, dentry);
return err;
}
/**
* cifsd_vfs_mkdir() - vfs helper for smb create directory
* @name: directory name
* @mode: directory create mode
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_mkdir(const char *name, umode_t mode)
{
struct path path;
struct dentry *dentry;
int err;
dentry = kern_path_create(AT_FDCWD, name, &path, LOOKUP_DIRECTORY);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
if (err != -EEXIST)
cifsd_err("path create failed for %s, err %d\n",
name, err);
return err;
}
mode |= S_IFDIR;
err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
if (err)
cifsd_err("mkdir(%s): creation failed (err:%d)\n", name, err);
done_path_create(&path, dentry);
return err;
}
static ssize_t cifsd_vfs_getcasexattr(struct dentry *dentry,
char *attr_name,
int attr_name_len,
char **attr_value)
{
char *name, *xattr_list = NULL;
ssize_t value_len = -ENOENT, xattr_list_len;
xattr_list_len = cifsd_vfs_listxattr(dentry,
&xattr_list,
XATTR_LIST_MAX);
if (xattr_list_len <= 0)
goto out;
for (name = xattr_list; name - xattr_list < xattr_list_len;
name += strlen(name) + 1) {
cifsd_debug("%s, len %zd\n", name, strlen(name));
if (strncasecmp(attr_name, name, attr_name_len))
continue;
value_len = cifsd_vfs_getxattr(dentry,
name,
attr_value);
if (value_len < 0)
cifsd_err("failed to get xattr in file\n");
break;
}
out:
if (xattr_list)
vfree(xattr_list);
return value_len;
}
static int cifsd_vfs_stream_read(struct cifsd_file *fp, char *buf, loff_t *pos,
size_t count)
{
ssize_t v_len;
char *stream_buf = NULL;
int err;
cifsd_debug("read stream data pos : %llu, count : %zd\n",
*pos, count);
v_len = cifsd_vfs_getcasexattr(fp->filp->f_path.dentry,
fp->stream.name,
fp->stream.size,
&stream_buf);
if (v_len == -ENOENT) {
cifsd_err("not found stream in xattr : %zd\n", v_len);
err = -ENOENT;
return err;
}
memcpy(buf, &stream_buf[*pos], count);
return v_len > count ? count : v_len;
}
/**
* check_lock_range() - vfs helper for smb byte range file locking
* @filp: the file to apply the lock to
* @start: lock start byte offset
* @end: lock end byte offset
* @type: byte range type read/write
*
* Return: 0 on success, otherwise error
*/
static int check_lock_range(struct file *filp,
loff_t start,
loff_t end,
unsigned char type)
{
struct file_lock *flock;
struct file_lock_context *ctx = file_inode(filp)->i_flctx;
int error = 0;
if (!ctx || list_empty_careful(&ctx->flc_posix))
return 0;
list_for_each_entry(flock, &ctx->flc_posix, fl_list) {
/* check conflict locks */
if (flock->fl_end >= start && end >= flock->fl_start) {
if (flock->fl_type == F_RDLCK) {
if (type == WRITE) {
cifsd_err("not allow write by shared lock\n");
error = 1;
goto out;
}
} else if (flock->fl_type == F_WRLCK) {
/* check owner in lock */
if (flock->fl_file != filp) {
error = 1;
cifsd_err("not allow rw access by exclusive lock from other opens\n");
goto out;
}
}
}
}
out:
return error;
}
/**
* cifsd_vfs_read() - vfs helper for smb file read
* @work: smb work
* @fid: file id of open file
* @count: read byte count
* @pos: file pos
*
* Return: number of read bytes on success, otherwise error
*/
int cifsd_vfs_read(struct cifsd_work *work,
struct cifsd_file *fp,
size_t count,
loff_t *pos)
{
struct file *filp;
ssize_t nbytes = 0;
char *rbuf, *name;
struct inode *inode;
char namebuf[NAME_MAX];
int ret;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
mm_segment_t old_fs;
#endif
rbuf = AUX_PAYLOAD(work);
filp = fp->filp;
inode = filp->f_path.dentry->d_inode;
if (S_ISDIR(inode->i_mode))
return -EISDIR;
if (unlikely(count == 0))
return 0;
if (work->conn->connection_type) {
if (!(fp->daccess & (FILE_READ_DATA_LE |
FILE_GENERIC_READ_LE | FILE_MAXIMAL_ACCESS_LE |
FILE_GENERIC_ALL_LE))) {
cifsd_err("no right to read(%s)\n", FP_FILENAME(fp));
return -EACCES;
}
}
if (fp->is_stream)
return cifsd_vfs_stream_read(fp, rbuf, pos, count);
ret = check_lock_range(filp, *pos, *pos + count - 1,
READ);
if (ret) {
cifsd_err("%s: unable to read due to lock\n",
__func__);
return -EAGAIN;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
old_fs = get_fs();
set_fs(KERNEL_DS);
nbytes = vfs_read(filp, rbuf, count, pos);
set_fs(old_fs);
#else
nbytes = kernel_read(filp, rbuf, count, pos);
#endif
if (nbytes < 0) {
name = d_path(&filp->f_path, namebuf, sizeof(namebuf));
if (IS_ERR(name))
name = "(error)";
cifsd_err("smb read failed for (%s), err = %zd\n",
name, nbytes);
return nbytes;
}
filp->f_pos = *pos;
return nbytes;
}
static int cifsd_vfs_stream_write(struct cifsd_file *fp, char *buf, loff_t *pos,
size_t count)
{
char *stream_buf = NULL, *wbuf;
size_t size, v_len;
int err = 0;
cifsd_debug("write stream data pos : %llu, count : %zd\n",
*pos, count);
size = *pos + count;
if (size > XATTR_SIZE_MAX) {
size = XATTR_SIZE_MAX;
count = (*pos + count) - XATTR_SIZE_MAX;
}
v_len = cifsd_vfs_getcasexattr(fp->filp->f_path.dentry,
fp->stream.name,
fp->stream.size,
&stream_buf);
if (v_len == -ENOENT) {
cifsd_err("not found stream in xattr : %zd\n", v_len);
err = -ENOENT;
goto out;
}
if (v_len < size) {
wbuf = cifsd_alloc(size);
if (!wbuf) {
err = -ENOMEM;
goto out;
}
if (v_len > 0)
memcpy(wbuf, stream_buf, v_len);
stream_buf = wbuf;
}
memcpy(&stream_buf[*pos], buf, count);
err = cifsd_vfs_setxattr(fp->filp->f_path.dentry,
fp->stream.name,
(void *)stream_buf,
size,
0);
if (err < 0)
goto out;
fp->filp->f_pos = *pos;
err = 0;
out:
cifsd_free(stream_buf);
return err;
}
/**
* cifsd_vfs_write() - vfs helper for smb file write
* @work: work
* @fid: file id of open file
* @buf: buf containing data for writing
* @count: read byte count
* @pos: file pos
* @sync: fsync after write
* @written: number of bytes written
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_write(struct cifsd_work *work, struct cifsd_file *fp,
char *buf, size_t count, loff_t *pos, bool sync, ssize_t *written)
{
struct cifsd_session *sess = work->sess;
struct file *filp;
loff_t offset = *pos;
int err = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
mm_segment_t old_fs;
#endif
if (sess->conn->connection_type) {
if (!(fp->daccess & (FILE_WRITE_DATA_LE |
FILE_GENERIC_WRITE_LE | FILE_MAXIMAL_ACCESS_LE |
FILE_GENERIC_ALL_LE))) {
cifsd_err("no right to write(%s)\n", FP_FILENAME(fp));
err = -EACCES;
goto out;
}
}
filp = fp->filp;
if (fp->is_stream) {
err = cifsd_vfs_stream_write(fp, buf, pos, count);
if (!err)
*written = count;
goto out;
}
err = check_lock_range(filp, *pos, *pos + count - 1,
WRITE);
if (err) {
cifsd_err("%s: unable to write due to lock\n",
__func__);
err = -EAGAIN;
goto out;
}
if (oplocks_enable) {
/* Do we need to break any of a levelII oplock? */
smb_break_all_levII_oplock(sess->conn, fp, 1);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
old_fs = get_fs();
set_fs(KERNEL_DS);
err = vfs_write(filp, buf, count, pos);
set_fs(old_fs);
#else
err = kernel_write(filp, buf, count, pos);
#endif
if (err < 0) {
cifsd_debug("smb write failed, err = %d\n", err);
goto out;
}
filp->f_pos = *pos;
*written = err;
err = 0;
if (sync) {
err = vfs_fsync_range(filp, offset, offset + *written, 0);
if (err < 0)
cifsd_err("fsync failed for filename = %s, err = %d\n",
FP_FILENAME(fp), err);
}
out:
return err;
}
#ifdef CONFIG_CIFS_INSECURE_SERVER
/**
* smb_check_attrs() - sanitize inode attributes
* @inode: inode
* @attrs: inode attributes
*/
void smb_check_attrs(struct inode *inode, struct iattr *attrs)
{
/* sanitize the mode change */
if (attrs->ia_valid & ATTR_MODE) {
attrs->ia_mode &= S_IALLUGO;
attrs->ia_mode |= (inode->i_mode & ~S_IALLUGO);
}
/* Revoke setuid/setgid on chown */
if (!S_ISDIR(inode->i_mode) &&
(((attrs->ia_valid & ATTR_UID) &&
!uid_eq(attrs->ia_uid, inode->i_uid)) ||
((attrs->ia_valid & ATTR_GID) &&
!gid_eq(attrs->ia_gid, inode->i_gid)))) {
attrs->ia_valid |= ATTR_KILL_PRIV;
if (attrs->ia_valid & ATTR_MODE) {
/* we're setting mode too, just clear the s*id bits */
attrs->ia_mode &= ~S_ISUID;
if (attrs->ia_mode & S_IXGRP)
attrs->ia_mode &= ~S_ISGID;
} else {
/* set ATTR_KILL_* bits and let VFS handle it */
attrs->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
}
}
}
/**
* cifsd_vfs_setattr() - vfs helper for smb setattr
* @work: work
* @name: file name
* @fid: file id of open file
* @attrs: inode attributes
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_setattr(struct cifsd_work *work, const char *name,
uint64_t fid, struct iattr *attrs)
{
struct cifsd_session *sess = work->sess;
struct file *filp;
struct dentry *dentry;
struct inode *inode;
struct path path;
bool update_size = false;
int err = 0;
struct cifsd_file *fp = NULL;
if (name) {
err = kern_path(name, 0, &path);
if (err) {
cifsd_debug("lookup failed for %s, err = %d\n",
name, err);
return -ENOENT;
}
dentry = path.dentry;
inode = dentry->d_inode;
} else {
fp = get_id_from_fidtable(sess, fid);
if (!fp) {
cifsd_err("failed to get filp for fid %llu\n", fid);
return -ENOENT;
}
filp = fp->filp;
dentry = filp->f_path.dentry;
inode = dentry->d_inode;
}
/* no need to update mode of symlink */
if (S_ISLNK(inode->i_mode))
attrs->ia_valid &= ~ATTR_MODE;
/* skip setattr, if nothing to update */
if (!attrs->ia_valid) {
err = 0;
goto out;
}
smb_check_attrs(inode, attrs);
if (attrs->ia_valid & ATTR_SIZE) {
err = get_write_access(inode);
if (err)
goto out;
err = locks_verify_truncate(inode, NULL, attrs->ia_size);
if (err) {
put_write_access(inode);
goto out;
}
update_size = true;
}
attrs->ia_valid |= ATTR_CTIME;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
inode_lock(inode);
err = notify_change(dentry, attrs, NULL);
inode_unlock(inode);
#else
mutex_lock(&inode->i_mutex);
err = notify_change(dentry, attrs, NULL);
mutex_unlock(&inode->i_mutex);
#endif
if (update_size)
put_write_access(inode);
if (!err) {
sync_inode_metadata(inode, 1);
cifsd_debug("fid %llu, setattr done\n", fid);
}
out:
if (name)
path_put(&path);
return err;
}
/**
* cifsd_vfs_getattr() - vfs helper for smb getattr
* @work: work
* @fid: file id of open file
* @attrs: inode attributes
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_getattr(struct cifsd_work *work, uint64_t fid,
struct kstat *stat)
{
struct cifsd_session *sess = work->sess;
struct file *filp;
struct cifsd_file *fp;
int err;
fp = get_id_from_fidtable(sess, fid);
if (!fp) {
cifsd_err("failed to get filp for fid %llu\n", fid);
return -ENOENT;
}
filp = fp->filp;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
err = vfs_getattr(&filp->f_path, stat, STATX_BASIC_STATS,
AT_STATX_SYNC_AS_STAT);
#else
err = vfs_getattr(&filp->f_path, stat);
#endif
if (err)
cifsd_err("getattr failed for fid %llu, err %d\n", fid, err);
return err;
}
/**
* cifsd_vfs_symlink() - vfs helper for creating smb symlink
* @name: source file name
* @symname: symlink name
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_symlink(const char *name, const char *symname)
{
struct path path;
struct dentry *dentry;
int err;
dentry = kern_path_create(AT_FDCWD, symname, &path, 0);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
cifsd_err("path create failed for %s, err %d\n", name, err);
return err;
}
err = vfs_symlink(dentry->d_parent->d_inode, dentry, name);
if (err && (err != -EEXIST || err != -ENOSPC))
cifsd_debug("failed to create symlink, err %d\n", err);
done_path_create(&path, dentry);
return err;
}
/**
* cifsd_vfs_readlink() - vfs helper for reading value of symlink
* @path: path of symlink
* @buf: destination buffer for symlink value
* @lenp: destination buffer length
*
* Return: symlink value length on success, otherwise error
*/
int cifsd_vfs_readlink(struct path *path, char *buf, int lenp)
{
struct inode *inode;
mm_segment_t old_fs;
int err;
if (!path)
return -ENOENT;
inode = path->dentry->d_inode;
if (!S_ISLNK(inode->i_mode))
return -EINVAL;
old_fs = get_fs();
set_fs(KERNEL_DS);
err = inode->i_op->readlink(path->dentry, (char __user *)buf, lenp);
set_fs(old_fs);
if (err < 0)
cifsd_err("readlink failed, err = %d\n", err);
return err;
}
#else
void smb_check_attrs(struct inode *inode, struct iattr *attrs)
{
}
int cifsd_vfs_setattr(struct cifsd_work *work, const char *name,
uint64_t fid, struct iattr *attrs)
{
return -ENOTSUPP;
}
int cifsd_vfs_getattr(struct cifsd_work *work, uint64_t fid,
struct kstat *stat)
{
return -ENOTSUPP;
}
int cifsd_vfs_symlink(const char *name, const char *symname)
{
return -ENOTSUPP;
}
int cifsd_vfs_readlink(struct path *path, char *buf, int lenp)
{
return -ENOTSUPP;
}
#endif
/**
* cifsd_vfs_fsync() - vfs helper for smb fsync
* @work: work
* @fid: file id of open file
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_fsync(struct cifsd_work *work, uint64_t fid, uint64_t p_id)
{
struct cifsd_session *sess = work->sess;
struct cifsd_file *fp;
int err;
fp = get_id_from_fidtable(sess, fid);
if (!fp) {
cifsd_err("failed to get filp for fid %llu\n", fid);
return -ENOENT;
}
if (fp->persistent_id != p_id) {
cifsd_err("persistent id mismatch : %llu, %llu\n",
fp->persistent_id, p_id);
return -ENOENT;
}
err = vfs_fsync(fp->filp, 0);
if (err < 0)
cifsd_err("smb fsync failed, err = %d\n", err);
return err;
}
/**
* cifsd_vfs_remove_file() - vfs helper for smb rmdir or unlink
* @name: absolute directory or file name
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_remove_file(char *name)
{
struct path parent;
struct dentry *dir, *dentry;
char *last;
int err = -ENOENT;
last = strrchr(name, '/');
if (last && last[1] != '\0') {
*last = '\0';
last++;
}
else {
cifsd_debug("can't get last component in path %s\n", name);
return -ENOENT;
}
err = kern_path(name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &parent);
if (err) {
cifsd_debug("can't get %s, err %d\n", name, err);
return err;
}
dir = parent.dentry;
if (!dir->d_inode)
goto out;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
inode_lock_nested(dir->d_inode, I_MUTEX_PARENT);
#else
mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
#endif
dentry = lookup_one_len(last, dir, strlen(last));
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
cifsd_debug("%s: lookup failed, err %d\n", last, err);
goto out_err;
}
if (!dentry->d_inode || !dentry->d_inode->i_nlink) {
dput(dentry);
err = -ENOENT;
goto out_err;
}
if (S_ISDIR(dentry->d_inode->i_mode)) {
err = vfs_rmdir(dir->d_inode, dentry);
if (err && err != -ENOTEMPTY)
cifsd_debug("%s: rmdir failed, err %d\n", name, err);
} else {
err = vfs_unlink(dir->d_inode, dentry, NULL);
if (err)
cifsd_debug("%s: unlink failed, err %d\n", name, err);
}
dput(dentry);
out_err:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
inode_unlock(dir->d_inode);
#else
mutex_unlock(&dir->d_inode->i_mutex);
#endif
out:
path_put(&parent);
return err;
}
/**
* cifsd_vfs_link() - vfs helper for creating smb hardlink
* @oldname: source file name
* @newname: hardlink name
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_link(const char *oldname, const char *newname)
{
struct path oldpath, newpath;
struct dentry *dentry;
int err;
err = kern_path(oldname, LOOKUP_FOLLOW, &oldpath);
if (err) {
cifsd_err("cannot get linux path for %s, err = %d\n",
oldname, err);
goto out1;
}
dentry = kern_path_create(AT_FDCWD, newname, &newpath,
LOOKUP_FOLLOW | LOOKUP_REVAL);
if (IS_ERR(dentry)) {
err = PTR_ERR(dentry);
cifsd_err("path create err for %s, err %d\n", newname, err);
goto out2;
}
err = -EXDEV;
if (oldpath.mnt != newpath.mnt) {
cifsd_err("vfs_link failed err %d\n", err);
goto out3;
}
err = vfs_link(oldpath.dentry, newpath.dentry->d_inode, dentry, NULL);
if (err)
cifsd_debug("vfs_link failed err %d\n", err);
out3:
done_path_create(&newpath, dentry);
out2:
path_put(&oldpath);
out1:
return err;
}
/**
* cifsd_vfs_rename() - vfs helper for smb rename
* @sess: session
* @abs_oldname: old filename
* @abs_newname: new filename
* @oldfid: file id of old file
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_rename(char *abs_oldname, char *abs_newname, struct cifsd_file *fp)
{
struct path oldpath_p, newpath_p;
struct dentry *dold, *dnew, *dold_p, *dnew_p, *trap, *child_de;
char *oldname = NULL, *newname = NULL;
int err;
if (abs_oldname) {
/* normal case: rename with source filename */
oldname = strrchr(abs_oldname, '/');
if (oldname && oldname[1] != '\0') {
*oldname = '\0';
oldname++;
}
else {
cifsd_err("can't get last component in path %s\n",
abs_oldname);
return -ENOENT;
}
err = kern_path(abs_oldname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
&oldpath_p);
if (err) {
cifsd_err("cannot get linux path for %s, err %d\n",
abs_oldname, err);
return -ENOENT;
}
dold_p = oldpath_p.dentry;
newname = strrchr(abs_newname, '/');
if (newname && newname[1] != '\0') {
*newname = '\0';
newname++;
}
else {
cifsd_err("can't get last component in path %s\n",
abs_newname);
err = -ENOMEM;
goto out1;
}
err = kern_path(abs_newname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
&newpath_p);
if (err) {
cifsd_err("cannot get linux path for %s, err = %d\n",
abs_newname, err);
goto out1;
}
dnew_p = newpath_p.dentry;
} else {
dold_p = fp->filp->f_path.dentry->d_parent;
newname = strrchr(abs_newname, '/');
if (newname && newname[1] != '\0') {
*newname = '\0';
newname++;
}
else {
cifsd_err("can't get last component in path %s\n",
abs_newname);
return -ENOMEM;
}
err = kern_path(abs_newname, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
&newpath_p);
if (err) {
cifsd_err("cannot get linux path for %s, err = %d\n",
abs_newname, err);
return err;
}
dnew_p = newpath_p.dentry;
}
cifsd_debug("oldname %s, newname %s\n", oldname, newname);
trap = lock_rename(dold_p, dnew_p);
if (abs_oldname) {
dold = lookup_one_len(oldname, dold_p, strlen(oldname));
err = PTR_ERR(dold);
if (IS_ERR(dold)) {
cifsd_err("%s lookup failed with error = %d\n",
oldname, err);
goto out2;
}
} else {
dold = fp->filp->f_path.dentry;
dget(dold);
}
spin_lock(&dold->d_lock);
list_for_each_entry(child_de, &dold->d_subdirs, d_child) {
struct cifsd_file *child_fp;
if (!child_de->d_inode)
continue;
child_fp = find_fp_using_inode(child_de->d_inode);
if (child_fp) {
cifsd_debug("not allow to rename dir with opening sub file\n");
err = -ENOTEMPTY;
spin_unlock(&dold->d_lock);
goto out3;
}
}
spin_unlock(&dold->d_lock);
err = -ENOENT;
if (!dold->d_inode)
goto out3;
err = -EINVAL;
if (dold == trap)
goto out3;
dnew = lookup_one_len(newname, dnew_p, strlen(newname));
err = PTR_ERR(dnew);
if (IS_ERR(dnew)) {
cifsd_err("%s lookup failed with error = %d\n",
newname, err);
goto out3;
}
err = -ENOTEMPTY;
if (dnew == trap)
goto out4;
err = vfs_rename(dold_p->d_inode, dold, dnew_p->d_inode, dnew, NULL, 0);
if (err)
cifsd_err("vfs_rename failed err %d\n", err);
out4:
dput(dnew);
out3:
dput(dold);
out2:
unlock_rename(dold_p, dnew_p);
path_put(&newpath_p);
out1:
if (abs_oldname)
path_put(&oldpath_p);
return err;
}
/**
* cifsd_vfs_truncate() - vfs helper for smb file truncate
* @work: work
* @name: old filename
* @fid: file id of old file
* @size: truncate to given size
*
* Return: 0 on success, otherwise error
*/
int cifsd_vfs_truncate(struct cifsd_work *work, const char *name,
struct cifsd_file *fp, loff_t size)
{
struct cifsd_session *sess = work->sess;
struct path path;
int err = 0;
struct inode *inode;
if (name) {
err = kern_path(name, 0, &path);
if (err) {
cifsd_err("cannot get linux path for %s, err %d\n",
name, err);
return err;
}
err = vfs_truncate(&path, size);
if (err)
cifsd_err("truncate failed for %s err %d\n",
name, err);
path_put(&path);
} else {
struct file *filp;
filp = fp->filp;
if (oplocks_enable) {
/* Do we need to break any of a levelII oplock? */
smb_break_all_levII_oplock(sess->conn, fp, 1);
} else {
inode = file_inode(filp);
if (size < inode->i_size) {
err = check_lock_range(filp, size,
inode->i_size - 1, WRITE);
} else {
err = check_lock_range(filp, inode->i_size,
size - 1, WRITE);