-
Notifications
You must be signed in to change notification settings - Fork 4
/
inode.c
575 lines (502 loc) · 15.7 KB
/
inode.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
/*
* Copyright (c) 1998-2017 Erez Zadok
* Copyright (c) 2009 Shrikar Archak
* Copyright (c) 2003-2017 Stony Brook University
* Copyright (c) 2003-2017 The Research Foundation of SUNY
* Copyright (c) 2018 Swapnil Ingle <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include "wrapfs.h"
static int wrapfs_create(struct inode *dir, struct dentry *dentry,
umode_t mode, bool want_excl)
{
int err;
struct dentry *lower_dentry;
struct dentry *lower_parent_dentry = NULL;
struct super_block *sb = dentry->d_sb;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_parent_dentry = lock_parent(lower_dentry);
if (d_really_is_positive(lower_dentry)) {
if (wrapfs_is_blocked(WRAPFS_SB(sb) , lower_dentry->d_name.name,
d_inode(lower_dentry)->i_ino)) {
err = -EPERM;
goto out;
}
}
err = vfs_create(d_inode(lower_parent_dentry), lower_dentry, mode,
want_excl);
if (err)
goto out;
err = wrapfs_interpose(dentry, dir->i_sb, &lower_path);
if (err)
goto out;
fsstack_copy_attr_times(dir, wrapfs_lower_inode(dir));
fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
out:
unlock_dir(lower_parent_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *new_dentry)
{
struct dentry *lower_old_dentry;
struct dentry *lower_new_dentry;
struct dentry *lower_dir_dentry;
u64 file_size_save;
int err;
struct path lower_old_path, lower_new_path;
file_size_save = i_size_read(d_inode(old_dentry));
wrapfs_get_lower_path(old_dentry, &lower_old_path);
wrapfs_get_lower_path(new_dentry, &lower_new_path);
lower_old_dentry = lower_old_path.dentry;
lower_new_dentry = lower_new_path.dentry;
lower_dir_dentry = lock_parent(lower_new_dentry);
err = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
lower_new_dentry, NULL);
if (err || !d_inode(lower_new_dentry))
goto out;
err = wrapfs_interpose(new_dentry, dir->i_sb, &lower_new_path);
if (err)
goto out;
fsstack_copy_attr_times(dir, d_inode(lower_new_dentry));
fsstack_copy_inode_size(dir, d_inode(lower_new_dentry));
set_nlink(d_inode(old_dentry),
wrapfs_lower_inode(d_inode(old_dentry))->i_nlink);
i_size_write(d_inode(new_dentry), file_size_save);
out:
unlock_dir(lower_dir_dentry);
wrapfs_put_lower_path(old_dentry, &lower_old_path);
wrapfs_put_lower_path(new_dentry, &lower_new_path);
return err;
}
static int wrapfs_unlink(struct inode *dir, struct dentry *dentry)
{
int err;
struct dentry *lower_dentry;
struct inode *lower_dir_inode = wrapfs_lower_inode(dir);
struct dentry *lower_dir_dentry;
struct path lower_path;
struct super_block *sb = dentry->d_sb;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
dget(lower_dentry);
lower_dir_dentry = lock_parent(lower_dentry);
err = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
/*
* Note: unlinking on top of NFS can cause silly-renamed files.
* Trying to delete such files results in EBUSY from NFS
* below. Silly-renamed files will get deleted by NFS later on, so
* we just need to detect them here and treat such EBUSY errors as
* if the upper file was successfully deleted.
*/
if (err == -EBUSY && lower_dentry->d_flags & DCACHE_NFSFS_RENAMED)
err = 0;
if (err)
goto out;
fsstack_copy_attr_times(dir, lower_dir_inode);
fsstack_copy_inode_size(dir, lower_dir_inode);
set_nlink(d_inode(dentry), wrapfs_lower_inode(d_inode(dentry))->i_nlink);
d_inode(dentry)->i_ctime = dir->i_ctime;
d_drop(dentry); /* this is needed, else LTP fails (VFS won't do it) */
wrapfs_remove_hnode(WRAPFS_SB(sb), dentry->d_name.name,
dentry->d_inode->i_ino);
out:
unlock_dir(lower_dir_dentry);
dput(lower_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_symlink(struct inode *dir, struct dentry *dentry,
const char *symname)
{
int err;
struct dentry *lower_dentry;
struct dentry *lower_parent_dentry = NULL;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_parent_dentry = lock_parent(lower_dentry);
err = vfs_symlink(d_inode(lower_parent_dentry), lower_dentry, symname);
if (err)
goto out;
err = wrapfs_interpose(dentry, dir->i_sb, &lower_path);
if (err)
goto out;
fsstack_copy_attr_times(dir, wrapfs_lower_inode(dir));
fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
out:
unlock_dir(lower_parent_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
int err;
struct dentry *lower_dentry;
struct dentry *lower_parent_dentry = NULL;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_parent_dentry = lock_parent(lower_dentry);
err = vfs_mkdir(d_inode(lower_parent_dentry), lower_dentry, mode);
if (err)
goto out;
err = wrapfs_interpose(dentry, dir->i_sb, &lower_path);
if (err)
goto out;
fsstack_copy_attr_times(dir, wrapfs_lower_inode(dir));
fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
/* update number of links on parent directory */
set_nlink(dir, wrapfs_lower_inode(dir)->i_nlink);
out:
unlock_dir(lower_parent_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_rmdir(struct inode *dir, struct dentry *dentry)
{
struct dentry *lower_dentry;
struct dentry *lower_dir_dentry;
int err;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_dir_dentry = lock_parent(lower_dentry);
err = vfs_rmdir(d_inode(lower_dir_dentry), lower_dentry);
if (err)
goto out;
d_drop(dentry); /* drop our dentry on success (why not VFS's job?) */
if (d_inode(dentry))
clear_nlink(d_inode(dentry));
fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
out:
unlock_dir(lower_dir_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
dev_t dev)
{
int err;
struct dentry *lower_dentry;
struct dentry *lower_parent_dentry = NULL;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_parent_dentry = lock_parent(lower_dentry);
err = vfs_mknod(d_inode(lower_parent_dentry), lower_dentry, mode, dev);
if (err)
goto out;
err = wrapfs_interpose(dentry, dir->i_sb, &lower_path);
if (err)
goto out;
fsstack_copy_attr_times(dir, wrapfs_lower_inode(dir));
fsstack_copy_inode_size(dir, d_inode(lower_parent_dentry));
out:
unlock_dir(lower_parent_dentry);
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
/*
* The locking rules in wrapfs_rename are complex. We could use a simpler
* superblock-level name-space lock for renames and copy-ups.
*/
static int wrapfs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
int err = 0;
struct dentry *lower_old_dentry = NULL;
struct dentry *lower_new_dentry = NULL;
struct dentry *lower_old_dir_dentry = NULL;
struct dentry *lower_new_dir_dentry = NULL;
struct dentry *trap = NULL;
struct path lower_old_path, lower_new_path;
wrapfs_get_lower_path(old_dentry, &lower_old_path);
wrapfs_get_lower_path(new_dentry, &lower_new_path);
lower_old_dentry = lower_old_path.dentry;
lower_new_dentry = lower_new_path.dentry;
lower_old_dir_dentry = dget_parent(lower_old_dentry);
lower_new_dir_dentry = dget_parent(lower_new_dentry);
trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
/* source should not be ancestor of target */
if (trap == lower_old_dentry) {
err = -EINVAL;
goto out;
}
/* target should not be ancestor of source */
if (trap == lower_new_dentry) {
err = -ENOTEMPTY;
goto out;
}
err = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
d_inode(lower_new_dir_dentry), lower_new_dentry,
NULL, 0);
if (err)
goto out;
fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
fsstack_copy_inode_size(new_dir, d_inode(lower_new_dir_dentry));
if (new_dir != old_dir) {
fsstack_copy_attr_all(old_dir,
d_inode(lower_old_dir_dentry));
fsstack_copy_inode_size(old_dir,
d_inode(lower_old_dir_dentry));
}
out:
unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
dput(lower_old_dir_dentry);
dput(lower_new_dir_dentry);
wrapfs_put_lower_path(old_dentry, &lower_old_path);
wrapfs_put_lower_path(new_dentry, &lower_new_path);
return err;
}
static int wrapfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
{
int err;
struct dentry *lower_dentry;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
if (!d_inode(lower_dentry)->i_op ||
!d_inode(lower_dentry)->i_op->readlink) {
err = -EINVAL;
goto out;
}
err = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
buf, bufsiz);
if (err < 0)
goto out;
fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry));
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int wrapfs_permission(struct inode *inode, int mask)
{
struct inode *lower_inode;
int err;
lower_inode = wrapfs_lower_inode(inode);
err = inode_permission(lower_inode, mask);
return err;
}
static int wrapfs_setattr(struct dentry *dentry, struct iattr *ia)
{
int err;
struct dentry *lower_dentry;
struct inode *inode;
struct inode *lower_inode;
struct path lower_path;
struct iattr lower_ia;
inode = d_inode(dentry);
#if 0
/*
* Check if user has permission to change inode. We don't check if
* this user can change the lower inode: that should happen when
* calling notify_change on the lower inode.
*/
err = setattr_prepare(dentry, ia);
if (err)
goto out_err;
#endif
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_inode = wrapfs_lower_inode(inode);
/* prepare our own lower struct iattr (with the lower file) */
memcpy(&lower_ia, ia, sizeof(lower_ia));
if (ia->ia_valid & ATTR_FILE)
lower_ia.ia_file = wrapfs_lower_file(ia->ia_file);
/*
* If shrinking, first truncate upper level to cancel writing dirty
* pages beyond the new eof; and also if its' maxbytes is more
* limiting (fail with -EFBIG before making any change to the lower
* level). There is no need to vmtruncate the upper level
* afterwards in the other cases: we fsstack_copy_inode_size from
* the lower level.
*/
if (ia->ia_valid & ATTR_SIZE) {
err = inode_newsize_ok(inode, ia->ia_size);
if (err)
goto out;
truncate_setsize(inode, ia->ia_size);
}
/*
* mode change is for clearing setuid/setgid bits. Allow lower fs
* to interpret this in its own way.
*/
if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
lower_ia.ia_valid &= ~ATTR_MODE;
/* notify the (possibly copied-up) lower inode */
/*
* Note: we use d_inode(lower_dentry), because lower_inode may be
* unlinked (no inode->i_sb and i_ino==0. This happens if someone
* tries to open(), unlink(), then ftruncate() a file.
*/
inode_lock(d_inode(lower_dentry));
err = notify_change(lower_dentry, &lower_ia, /* note: lower_ia */
NULL);
inode_unlock(d_inode(lower_dentry));
if (err)
goto out;
/* get attributes from the lower inode */
fsstack_copy_attr_all(inode, lower_inode);
/*
* Not running fsstack_copy_inode_size(inode, lower_inode), because
* VFS should update our inode size, and notify_change on
* lower_inode should update its size.
*/
out:
wrapfs_put_lower_path(dentry, &lower_path);
#if 0
out_err:
#endif
return err;
}
static int wrapfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
struct kstat *stat)
{
int err;
struct kstat lower_stat;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
err = vfs_getattr(&lower_path, &lower_stat);
if (err)
goto out;
fsstack_copy_attr_all(d_inode(dentry),
d_inode(lower_path.dentry));
generic_fillattr(d_inode(dentry), stat);
stat->blocks = lower_stat.blocks;
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int
wrapfs_setxattr(struct dentry *dentry, const char *name, const void *value,
size_t size, int flags)
{
int err; struct dentry *lower_dentry;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
err = vfs_setxattr(lower_dentry, name, value, size, flags);
if (err)
goto out;
fsstack_copy_attr_all(d_inode(dentry),
d_inode(lower_path.dentry));
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static ssize_t
wrapfs_getxattr(struct dentry *dentry, const char *name,
void *buffer, size_t size)
{
int err;
struct dentry *lower_dentry;
struct inode *lower_inode;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_inode = d_inode(lower_dentry);
err = vfs_getxattr(lower_dentry, name, buffer, size);
if (err)
goto out;
fsstack_copy_attr_atime(d_inode(dentry),
d_inode(lower_path.dentry));
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static ssize_t
wrapfs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
{
int err;
struct dentry *lower_dentry;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
err = vfs_listxattr(lower_dentry, buffer, buffer_size);
if (err)
goto out;
fsstack_copy_attr_atime(d_inode(dentry),
d_inode(lower_path.dentry));
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
static int
wrapfs_removexattr(struct dentry *dentry, const char *name)
{
int err;
struct dentry *lower_dentry;
struct inode *lower_inode;
struct path lower_path;
wrapfs_get_lower_path(dentry, &lower_path);
lower_dentry = lower_path.dentry;
lower_inode = d_inode(lower_dentry);;
err = vfs_removexattr(lower_dentry, name);
if (err)
goto out;
fsstack_copy_attr_all(d_inode(dentry), lower_inode);
out:
wrapfs_put_lower_path(dentry, &lower_path);
return err;
}
const struct inode_operations wrapfs_symlink_iops = {
.readlink = wrapfs_readlink,
.permission = wrapfs_permission,
.setattr = wrapfs_setattr,
.getattr = wrapfs_getattr,
.listxattr = wrapfs_listxattr,
};
const struct inode_operations wrapfs_dir_iops = {
.create = wrapfs_create,
.lookup = wrapfs_lookup,
.link = wrapfs_link,
.unlink = wrapfs_unlink,
.symlink = wrapfs_symlink,
.mkdir = wrapfs_mkdir,
.rmdir = wrapfs_rmdir,
.mknod = wrapfs_mknod,
.rename = wrapfs_rename,
.permission = wrapfs_permission,
.setattr = wrapfs_setattr,
.getattr = wrapfs_getattr,
.listxattr = wrapfs_listxattr,
};
const struct inode_operations wrapfs_main_iops = {
.permission = wrapfs_permission,
.setattr = wrapfs_setattr,
.getattr = wrapfs_getattr,
.listxattr = wrapfs_listxattr,
};
static int wrapfs_xattr_get(const struct xattr_handler *handler,
struct dentry *dentry, const char *name,
void *buffer, size_t size)
{
return wrapfs_getxattr(dentry, name, buffer, size);
}
static int wrapfs_xattr_set(const struct xattr_handler *handler,
struct dentry *dentry, const char *name,
const void *value, size_t size, int flags)
{
if (value)
return wrapfs_setxattr(dentry, name, value, size, flags);
BUG_ON(flags != XATTR_REPLACE);
return wrapfs_removexattr(dentry, name);
}
const struct xattr_handler wrapfs_xattr_handler = {
.prefix = "", /* match anything */
.get = wrapfs_xattr_get,
.set = wrapfs_xattr_set,
};
const struct xattr_handler *wrapfs_xattr_handlers[] = {
&wrapfs_xattr_handler,
NULL
};