0eb1c59f5ab835c1983267d3f7e026ce77c69565
[deliverable/linux.git] / fs / nfsd / nfsfh.c
1 /*
2 * linux/fs/nfsd/nfsfh.c
3 *
4 * NFS server file handle treatment.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 * Portions Copyright (C) 1999 G. Allen Morris III <gam3@acm.org>
8 * Extensive rewrite by Neil Brown <neilb@cse.unsw.edu.au> Southern-Spring 1999
9 * ... and again Southern-Winter 2001 to support export_operations
10 */
11
12 #include <linux/exportfs.h>
13
14 #include <linux/sunrpc/svcauth_gss.h>
15 #include "nfsd.h"
16 #include "vfs.h"
17 #include "auth.h"
18
19 #define NFSDDBG_FACILITY NFSDDBG_FH
20
21
22 /*
23 * our acceptability function.
24 * if NOSUBTREECHECK, accept anything
25 * if not, require that we can walk up to exp->ex_dentry
26 * doing some checks on the 'x' bits
27 */
28 static int nfsd_acceptable(void *expv, struct dentry *dentry)
29 {
30 struct svc_export *exp = expv;
31 int rv;
32 struct dentry *tdentry;
33 struct dentry *parent;
34
35 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
36 return 1;
37
38 tdentry = dget(dentry);
39 while (tdentry != exp->ex_path.dentry && !IS_ROOT(tdentry)) {
40 /* make sure parents give x permission to user */
41 int err;
42 parent = dget_parent(tdentry);
43 err = inode_permission(parent->d_inode, MAY_EXEC);
44 if (err < 0) {
45 dput(parent);
46 break;
47 }
48 dput(tdentry);
49 tdentry = parent;
50 }
51 if (tdentry != exp->ex_path.dentry)
52 dprintk("nfsd_acceptable failed at %p %s\n", tdentry, tdentry->d_name.name);
53 rv = (tdentry == exp->ex_path.dentry);
54 dput(tdentry);
55 return rv;
56 }
57
58 /* Type check. The correct error return for type mismatches does not seem to be
59 * generally agreed upon. SunOS seems to use EISDIR if file isn't S_IFREG; a
60 * comment in the NFSv3 spec says this is incorrect (implementation notes for
61 * the write call).
62 */
63 static inline __be32
64 nfsd_mode_check(struct svc_rqst *rqstp, umode_t mode, int type)
65 {
66 /* Type can be negative when creating hardlinks - not to a dir */
67 if (type > 0 && (mode & S_IFMT) != type) {
68 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
69 return nfserr_symlink;
70 else if (type == S_IFDIR)
71 return nfserr_notdir;
72 else if ((mode & S_IFMT) == S_IFDIR)
73 return nfserr_isdir;
74 else
75 return nfserr_inval;
76 }
77 if (type < 0 && (mode & S_IFMT) == -type) {
78 if (rqstp->rq_vers == 4 && (mode & S_IFMT) == S_IFLNK)
79 return nfserr_symlink;
80 else if (type == -S_IFDIR)
81 return nfserr_isdir;
82 else
83 return nfserr_notdir;
84 }
85 return 0;
86 }
87
88 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
89 struct svc_export *exp)
90 {
91 /* Check if the request originated from a secure port. */
92 if (!rqstp->rq_secure && EX_SECURE(exp)) {
93 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
94 dprintk(KERN_WARNING
95 "nfsd: request from insecure port %s!\n",
96 svc_print_addr(rqstp, buf, sizeof(buf)));
97 return nfserr_perm;
98 }
99
100 /* Set user creds for this exportpoint */
101 return nfserrno(nfsd_setuser(rqstp, exp));
102 }
103
104 /*
105 * Use the given filehandle to look up the corresponding export and
106 * dentry. On success, the results are used to set fh_export and
107 * fh_dentry.
108 */
109 static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp)
110 {
111 struct knfsd_fh *fh = &fhp->fh_handle;
112 struct fid *fid = NULL, sfid;
113 struct svc_export *exp;
114 struct dentry *dentry;
115 int fileid_type;
116 int data_left = fh->fh_size/4;
117 __be32 error;
118
119 error = nfserr_stale;
120 if (rqstp->rq_vers > 2)
121 error = nfserr_badhandle;
122 if (rqstp->rq_vers == 4 && fh->fh_size == 0)
123 return nfserr_nofilehandle;
124
125 if (fh->fh_version == 1) {
126 int len;
127
128 if (--data_left < 0)
129 return error;
130 if (fh->fh_auth_type != 0)
131 return error;
132 len = key_len(fh->fh_fsid_type) / 4;
133 if (len == 0)
134 return error;
135 if (fh->fh_fsid_type == FSID_MAJOR_MINOR) {
136 /* deprecated, convert to type 3 */
137 len = key_len(FSID_ENCODE_DEV)/4;
138 fh->fh_fsid_type = FSID_ENCODE_DEV;
139 fh->fh_fsid[0] = new_encode_dev(MKDEV(ntohl(fh->fh_fsid[0]), ntohl(fh->fh_fsid[1])));
140 fh->fh_fsid[1] = fh->fh_fsid[2];
141 }
142 data_left -= len;
143 if (data_left < 0)
144 return error;
145 exp = rqst_exp_find(rqstp, fh->fh_fsid_type, fh->fh_auth);
146 fid = (struct fid *)(fh->fh_auth + len);
147 } else {
148 __u32 tfh[2];
149 dev_t xdev;
150 ino_t xino;
151
152 if (fh->fh_size != NFS_FHSIZE)
153 return error;
154 /* assume old filehandle format */
155 xdev = old_decode_dev(fh->ofh_xdev);
156 xino = u32_to_ino_t(fh->ofh_xino);
157 mk_fsid(FSID_DEV, tfh, xdev, xino, 0, NULL);
158 exp = rqst_exp_find(rqstp, FSID_DEV, tfh);
159 }
160
161 error = nfserr_stale;
162 if (PTR_ERR(exp) == -ENOENT)
163 return error;
164
165 if (IS_ERR(exp))
166 return nfserrno(PTR_ERR(exp));
167
168 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK) {
169 /* Elevate privileges so that the lack of 'r' or 'x'
170 * permission on some parent directory will
171 * not stop exportfs_decode_fh from being able
172 * to reconnect a directory into the dentry cache.
173 * The same problem can affect "SUBTREECHECK" exports,
174 * but as nfsd_acceptable depends on correct
175 * access control settings being in effect, we cannot
176 * fix that case easily.
177 */
178 struct cred *new = prepare_creds();
179 if (!new)
180 return nfserrno(-ENOMEM);
181 new->cap_effective =
182 cap_raise_nfsd_set(new->cap_effective,
183 new->cap_permitted);
184 put_cred(override_creds(new));
185 put_cred(new);
186 } else {
187 error = nfsd_setuser_and_check_port(rqstp, exp);
188 if (error)
189 goto out;
190 }
191
192 /*
193 * Look up the dentry using the NFS file handle.
194 */
195 error = nfserr_stale;
196 if (rqstp->rq_vers > 2)
197 error = nfserr_badhandle;
198
199 if (fh->fh_version != 1) {
200 sfid.i32.ino = fh->ofh_ino;
201 sfid.i32.gen = fh->ofh_generation;
202 sfid.i32.parent_ino = fh->ofh_dirino;
203 fid = &sfid;
204 data_left = 3;
205 if (fh->ofh_dirino == 0)
206 fileid_type = FILEID_INO32_GEN;
207 else
208 fileid_type = FILEID_INO32_GEN_PARENT;
209 } else
210 fileid_type = fh->fh_fileid_type;
211
212 if (fileid_type == FILEID_ROOT)
213 dentry = dget(exp->ex_path.dentry);
214 else {
215 dentry = exportfs_decode_fh(exp->ex_path.mnt, fid,
216 data_left, fileid_type,
217 nfsd_acceptable, exp);
218 }
219 if (dentry == NULL)
220 goto out;
221 if (IS_ERR(dentry)) {
222 if (PTR_ERR(dentry) != -EINVAL)
223 error = nfserrno(PTR_ERR(dentry));
224 goto out;
225 }
226
227 if (S_ISDIR(dentry->d_inode->i_mode) &&
228 (dentry->d_flags & DCACHE_DISCONNECTED)) {
229 printk("nfsd: find_fh_dentry returned a DISCONNECTED directory: %s/%s\n",
230 dentry->d_parent->d_name.name, dentry->d_name.name);
231 }
232
233 fhp->fh_dentry = dentry;
234 fhp->fh_export = exp;
235 return 0;
236 out:
237 exp_put(exp);
238 return error;
239 }
240
241 /**
242 * fh_verify - filehandle lookup and access checking
243 * @rqstp: pointer to current rpc request
244 * @fhp: filehandle to be verified
245 * @type: expected type of object pointed to by filehandle
246 * @access: type of access needed to object
247 *
248 * Look up a dentry from the on-the-wire filehandle, check the client's
249 * access to the export, and set the current task's credentials.
250 *
251 * Regardless of success or failure of fh_verify(), fh_put() should be
252 * called on @fhp when the caller is finished with the filehandle.
253 *
254 * fh_verify() may be called multiple times on a given filehandle, for
255 * example, when processing an NFSv4 compound. The first call will look
256 * up a dentry using the on-the-wire filehandle. Subsequent calls will
257 * skip the lookup and just perform the other checks and possibly change
258 * the current task's credentials.
259 *
260 * @type specifies the type of object expected using one of the S_IF*
261 * constants defined in include/linux/stat.h. The caller may use zero
262 * to indicate that it doesn't care, or a negative integer to indicate
263 * that it expects something not of the given type.
264 *
265 * @access is formed from the NFSD_MAY_* constants defined in
266 * include/linux/nfsd/nfsd.h.
267 */
268 __be32
269 fh_verify(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, int access)
270 {
271 struct svc_export *exp;
272 struct dentry *dentry;
273 __be32 error;
274
275 dprintk("nfsd: fh_verify(%s)\n", SVCFH_fmt(fhp));
276
277 if (!fhp->fh_dentry) {
278 error = nfsd_set_fh_dentry(rqstp, fhp);
279 if (error)
280 goto out;
281 }
282 dentry = fhp->fh_dentry;
283 exp = fhp->fh_export;
284 /*
285 * We still have to do all these permission checks, even when
286 * fh_dentry is already set:
287 * - fh_verify may be called multiple times with different
288 * "access" arguments (e.g. nfsd_proc_create calls
289 * fh_verify(...,NFSD_MAY_EXEC) first, then later (in
290 * nfsd_create) calls fh_verify(...,NFSD_MAY_CREATE).
291 * - in the NFSv4 case, the filehandle may have been filled
292 * in by fh_compose, and given a dentry, but further
293 * compound operations performed with that filehandle
294 * still need permissions checks. In the worst case, a
295 * mountpoint crossing may have changed the export
296 * options, and we may now need to use a different uid
297 * (for example, if different id-squashing options are in
298 * effect on the new filesystem).
299 */
300 error = nfsd_setuser_and_check_port(rqstp, exp);
301 if (error)
302 goto out;
303
304 error = nfsd_mode_check(rqstp, dentry->d_inode->i_mode, type);
305 if (error)
306 goto out;
307
308 /*
309 * pseudoflavor restrictions are not enforced on NLM,
310 * which clients virtually always use auth_sys for,
311 * even while using RPCSEC_GSS for NFS.
312 */
313 if (access & NFSD_MAY_LOCK)
314 goto skip_pseudoflavor_check;
315 /*
316 * Clients may expect to be able to use auth_sys during mount,
317 * even if they use gss for everything else; see section 2.3.2
318 * of rfc 2623.
319 */
320 if (access & NFSD_MAY_BYPASS_GSS_ON_ROOT
321 && exp->ex_path.dentry == dentry)
322 goto skip_pseudoflavor_check;
323
324 error = check_nfsd_access(exp, rqstp);
325 if (error)
326 goto out;
327
328 skip_pseudoflavor_check:
329 /* Finally, check access permissions. */
330 error = nfsd_permission(rqstp, exp, dentry, access);
331
332 if (error) {
333 dprintk("fh_verify: %s/%s permission failure, "
334 "acc=%x, error=%d\n",
335 dentry->d_parent->d_name.name,
336 dentry->d_name.name,
337 access, ntohl(error));
338 }
339 out:
340 if (error == nfserr_stale)
341 nfsdstats.fh_stale++;
342 return error;
343 }
344
345
346 /*
347 * Compose a file handle for an NFS reply.
348 *
349 * Note that when first composed, the dentry may not yet have
350 * an inode. In this case a call to fh_update should be made
351 * before the fh goes out on the wire ...
352 */
353 static void _fh_update(struct svc_fh *fhp, struct svc_export *exp,
354 struct dentry *dentry)
355 {
356 if (dentry != exp->ex_path.dentry) {
357 struct fid *fid = (struct fid *)
358 (fhp->fh_handle.fh_auth + fhp->fh_handle.fh_size/4 - 1);
359 int maxsize = (fhp->fh_maxsize - fhp->fh_handle.fh_size)/4;
360 int subtreecheck = !(exp->ex_flags & NFSEXP_NOSUBTREECHECK);
361
362 fhp->fh_handle.fh_fileid_type =
363 exportfs_encode_fh(dentry, fid, &maxsize, subtreecheck);
364 fhp->fh_handle.fh_size += maxsize * 4;
365 } else {
366 fhp->fh_handle.fh_fileid_type = FILEID_ROOT;
367 }
368 }
369
370 /*
371 * for composing old style file handles
372 */
373 static inline void _fh_update_old(struct dentry *dentry,
374 struct svc_export *exp,
375 struct knfsd_fh *fh)
376 {
377 fh->ofh_ino = ino_t_to_u32(dentry->d_inode->i_ino);
378 fh->ofh_generation = dentry->d_inode->i_generation;
379 if (S_ISDIR(dentry->d_inode->i_mode) ||
380 (exp->ex_flags & NFSEXP_NOSUBTREECHECK))
381 fh->ofh_dirino = 0;
382 }
383
384 static bool is_root_export(struct svc_export *exp)
385 {
386 return exp->ex_path.dentry == exp->ex_path.dentry->d_sb->s_root;
387 }
388
389 static struct super_block *exp_sb(struct svc_export *exp)
390 {
391 return exp->ex_path.dentry->d_inode->i_sb;
392 }
393
394 static bool fsid_type_ok_for_exp(u8 fsid_type, struct svc_export *exp)
395 {
396 switch (fsid_type) {
397 case FSID_DEV:
398 if (!old_valid_dev(exp_sb(exp)->s_dev))
399 return 0;
400 /* FALL THROUGH */
401 case FSID_MAJOR_MINOR:
402 case FSID_ENCODE_DEV:
403 return exp_sb(exp)->s_type->fs_flags & FS_REQUIRES_DEV;
404 case FSID_NUM:
405 return exp->ex_flags & NFSEXP_FSID;
406 case FSID_UUID8:
407 case FSID_UUID16:
408 if (!is_root_export(exp))
409 return 0;
410 /* fall through */
411 case FSID_UUID4_INUM:
412 case FSID_UUID16_INUM:
413 return exp->ex_uuid != NULL;
414 }
415 return 1;
416 }
417
418
419 static void set_version_and_fsid_type(struct svc_fh *fhp, struct svc_export *exp, struct svc_fh *ref_fh)
420 {
421 u8 version;
422 u8 fsid_type;
423 retry:
424 version = 1;
425 if (ref_fh && ref_fh->fh_export == exp) {
426 version = ref_fh->fh_handle.fh_version;
427 fsid_type = ref_fh->fh_handle.fh_fsid_type;
428
429 ref_fh = NULL;
430
431 switch (version) {
432 case 0xca:
433 fsid_type = FSID_DEV;
434 break;
435 case 1:
436 break;
437 default:
438 goto retry;
439 }
440
441 /*
442 * As the fsid -> filesystem mapping was guided by
443 * user-space, there is no guarantee that the filesystem
444 * actually supports that fsid type. If it doesn't we
445 * loop around again without ref_fh set.
446 */
447 if (!fsid_type_ok_for_exp(fsid_type, exp))
448 goto retry;
449 } else if (exp->ex_flags & NFSEXP_FSID) {
450 fsid_type = FSID_NUM;
451 } else if (exp->ex_uuid) {
452 if (fhp->fh_maxsize >= 64) {
453 if (is_root_export(exp))
454 fsid_type = FSID_UUID16;
455 else
456 fsid_type = FSID_UUID16_INUM;
457 } else {
458 if (is_root_export(exp))
459 fsid_type = FSID_UUID8;
460 else
461 fsid_type = FSID_UUID4_INUM;
462 }
463 } else if (!old_valid_dev(exp_sb(exp)->s_dev))
464 /* for newer device numbers, we must use a newer fsid format */
465 fsid_type = FSID_ENCODE_DEV;
466 else
467 fsid_type = FSID_DEV;
468 fhp->fh_handle.fh_version = version;
469 if (version)
470 fhp->fh_handle.fh_fsid_type = fsid_type;
471 }
472
473 __be32
474 fh_compose(struct svc_fh *fhp, struct svc_export *exp, struct dentry *dentry,
475 struct svc_fh *ref_fh)
476 {
477 /* ref_fh is a reference file handle.
478 * if it is non-null and for the same filesystem, then we should compose
479 * a filehandle which is of the same version, where possible.
480 * Currently, that means that if ref_fh->fh_handle.fh_version == 0xca
481 * Then create a 32byte filehandle using nfs_fhbase_old
482 *
483 */
484
485 struct inode * inode = dentry->d_inode;
486 struct dentry *parent = dentry->d_parent;
487 __u32 *datap;
488 dev_t ex_dev = exp_sb(exp)->s_dev;
489
490 dprintk("nfsd: fh_compose(exp %02x:%02x/%ld %s/%s, ino=%ld)\n",
491 MAJOR(ex_dev), MINOR(ex_dev),
492 (long) exp->ex_path.dentry->d_inode->i_ino,
493 parent->d_name.name, dentry->d_name.name,
494 (inode ? inode->i_ino : 0));
495
496 /* Choose filehandle version and fsid type based on
497 * the reference filehandle (if it is in the same export)
498 * or the export options.
499 */
500 set_version_and_fsid_type(fhp, exp, ref_fh);
501
502 if (ref_fh == fhp)
503 fh_put(ref_fh);
504
505 if (fhp->fh_locked || fhp->fh_dentry) {
506 printk(KERN_ERR "fh_compose: fh %s/%s not initialized!\n",
507 parent->d_name.name, dentry->d_name.name);
508 }
509 if (fhp->fh_maxsize < NFS_FHSIZE)
510 printk(KERN_ERR "fh_compose: called with maxsize %d! %s/%s\n",
511 fhp->fh_maxsize,
512 parent->d_name.name, dentry->d_name.name);
513
514 fhp->fh_dentry = dget(dentry); /* our internal copy */
515 fhp->fh_export = exp;
516 cache_get(&exp->h);
517
518 if (fhp->fh_handle.fh_version == 0xca) {
519 /* old style filehandle please */
520 memset(&fhp->fh_handle.fh_base, 0, NFS_FHSIZE);
521 fhp->fh_handle.fh_size = NFS_FHSIZE;
522 fhp->fh_handle.ofh_dcookie = 0xfeebbaca;
523 fhp->fh_handle.ofh_dev = old_encode_dev(ex_dev);
524 fhp->fh_handle.ofh_xdev = fhp->fh_handle.ofh_dev;
525 fhp->fh_handle.ofh_xino =
526 ino_t_to_u32(exp->ex_path.dentry->d_inode->i_ino);
527 fhp->fh_handle.ofh_dirino = ino_t_to_u32(parent_ino(dentry));
528 if (inode)
529 _fh_update_old(dentry, exp, &fhp->fh_handle);
530 } else {
531 int len;
532 fhp->fh_handle.fh_auth_type = 0;
533 datap = fhp->fh_handle.fh_auth+0;
534 mk_fsid(fhp->fh_handle.fh_fsid_type, datap, ex_dev,
535 exp->ex_path.dentry->d_inode->i_ino,
536 exp->ex_fsid, exp->ex_uuid);
537
538 len = key_len(fhp->fh_handle.fh_fsid_type);
539 datap += len/4;
540 fhp->fh_handle.fh_size = 4 + len;
541
542 if (inode)
543 _fh_update(fhp, exp, dentry);
544 if (fhp->fh_handle.fh_fileid_type == 255) {
545 fh_put(fhp);
546 return nfserr_opnotsupp;
547 }
548 }
549
550 return 0;
551 }
552
553 /*
554 * Update file handle information after changing a dentry.
555 * This is only called by nfsd_create, nfsd_create_v3 and nfsd_proc_create
556 */
557 __be32
558 fh_update(struct svc_fh *fhp)
559 {
560 struct dentry *dentry;
561
562 if (!fhp->fh_dentry)
563 goto out_bad;
564
565 dentry = fhp->fh_dentry;
566 if (!dentry->d_inode)
567 goto out_negative;
568 if (fhp->fh_handle.fh_version != 1) {
569 _fh_update_old(dentry, fhp->fh_export, &fhp->fh_handle);
570 } else {
571 if (fhp->fh_handle.fh_fileid_type != FILEID_ROOT)
572 goto out;
573
574 _fh_update(fhp, fhp->fh_export, dentry);
575 if (fhp->fh_handle.fh_fileid_type == 255)
576 return nfserr_opnotsupp;
577 }
578 out:
579 return 0;
580
581 out_bad:
582 printk(KERN_ERR "fh_update: fh not verified!\n");
583 goto out;
584 out_negative:
585 printk(KERN_ERR "fh_update: %s/%s still negative!\n",
586 dentry->d_parent->d_name.name, dentry->d_name.name);
587 goto out;
588 }
589
590 /*
591 * Release a file handle.
592 */
593 void
594 fh_put(struct svc_fh *fhp)
595 {
596 struct dentry * dentry = fhp->fh_dentry;
597 struct svc_export * exp = fhp->fh_export;
598 if (dentry) {
599 fh_unlock(fhp);
600 fhp->fh_dentry = NULL;
601 dput(dentry);
602 #ifdef CONFIG_NFSD_V3
603 fhp->fh_pre_saved = 0;
604 fhp->fh_post_saved = 0;
605 #endif
606 }
607 if (exp) {
608 cache_put(&exp->h, &svc_export_cache);
609 fhp->fh_export = NULL;
610 }
611 return;
612 }
613
614 /*
615 * Shorthand for dprintk()'s
616 */
617 char * SVCFH_fmt(struct svc_fh *fhp)
618 {
619 struct knfsd_fh *fh = &fhp->fh_handle;
620
621 static char buf[80];
622 sprintf(buf, "%d: %08x %08x %08x %08x %08x %08x",
623 fh->fh_size,
624 fh->fh_base.fh_pad[0],
625 fh->fh_base.fh_pad[1],
626 fh->fh_base.fh_pad[2],
627 fh->fh_base.fh_pad[3],
628 fh->fh_base.fh_pad[4],
629 fh->fh_base.fh_pad[5]);
630 return buf;
631 }
632
633 enum fsid_source fsid_source(struct svc_fh *fhp)
634 {
635 if (fhp->fh_handle.fh_version != 1)
636 return FSIDSOURCE_DEV;
637 switch(fhp->fh_handle.fh_fsid_type) {
638 case FSID_DEV:
639 case FSID_ENCODE_DEV:
640 case FSID_MAJOR_MINOR:
641 if (exp_sb(fhp->fh_export)->s_type->fs_flags & FS_REQUIRES_DEV)
642 return FSIDSOURCE_DEV;
643 break;
644 case FSID_NUM:
645 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
646 return FSIDSOURCE_FSID;
647 break;
648 default:
649 break;
650 }
651 /* either a UUID type filehandle, or the filehandle doesn't
652 * match the export.
653 */
654 if (fhp->fh_export->ex_flags & NFSEXP_FSID)
655 return FSIDSOURCE_FSID;
656 if (fhp->fh_export->ex_uuid)
657 return FSIDSOURCE_UUID;
658 return FSIDSOURCE_DEV;
659 }
This page took 0.052953 seconds and 4 git commands to generate.