fs: rename AT_* to VATTR_*
[unleashed/lotheac.git] / usr / src / stand / lib / fs / nfs / nfs4ops.c
bloba93f26652a365b4b24cca00255b0b576a5b8bfee
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Simple nfs V4 ops
28 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <rpc/types.h>
31 #include <rpc/auth.h>
32 #include <sys/t_lock.h>
33 #include "clnt.h"
34 #include <sys/fcntl.h>
35 #include <sys/vfs.h>
36 #include <errno.h>
37 #include <sys/promif.h>
38 #include <rpc/xdr.h>
39 #include "nfs_inet.h"
40 #include <sys/stat.h>
41 #include <sys/bootvfs.h>
42 #include <sys/bootdebug.h>
43 #include <sys/salib.h>
44 #include <sys/sacache.h>
45 #include <rpc/rpc.h>
46 #include "brpc.h"
47 #include <rpcsvc/nfs4_prot.h>
49 #define dprintf if (boothowto & RB_DEBUG) printf
51 static struct timeval zero_timeout = {0, 0}; /* default */
54 * NFS Version 4 specific functions
57 ssize_t
58 nfs4read(struct nfs_file *filep, char *buf, size_t size)
60 enum clnt_stat status;
61 read4arg_t readargs;
62 read4res_t readres;
63 char *buf_offset;
64 uint_t count = 0;
65 uint_t readcnt = 0;
66 bool_t done = FALSE;
67 struct timeval timeout;
68 int framing_errs = 0;
69 static uint_t pos;
70 static char ind[] = "|/-\\";
71 static int blks_read;
72 utf8string str;
73 char tagname[] = "inetboot read";
75 bzero(&readres, sizeof (readres));
77 str.utf8string_len = sizeof (tagname) - 1;
78 str.utf8string_val = tagname;
81 * read
83 buf_offset = buf;
85 if (nfs_readsize == 0)
86 nfs_readsize = READ4_SIZE;
88 if (size < nfs_readsize)
89 readargs.r_count = size;
90 else
91 readargs.r_count = nfs_readsize;
93 if (filep->fh.fh4.len > 0)
94 compound_init(&readargs.r_arg, &str, 0, 2, &filep->fh.fh4);
95 else
96 compound_init(&readargs.r_arg, &str, 0, 2, NULL);
98 readargs.r_opread = OP_READ;
100 * zero out the stateid field
102 bzero(&readargs.r_stateid, sizeof (readargs.r_stateid));
103 readargs.r_offset = filep->offset;
105 do {
106 readres.r_data_val = buf_offset;
108 if ((count + readargs.r_count) > size)
109 readargs.r_count = size - count;
111 timeout.tv_sec = NFS_REXMIT_MIN;
112 timeout.tv_usec = 0;
114 do {
115 status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND,
116 xdr_read4_args, (caddr_t)&readargs,
117 xdr_read4_res, (caddr_t)&readres,
118 timeout);
120 if (status == RPC_TIMEDOUT) {
121 dprintf("NFS read(%d) timed out. Retrying...\n", readargs.r_count);
122 if (errno == ETIMEDOUT)
123 framing_errs++;
125 if (framing_errs > NFS_MAX_FERRS &&
126 readargs.r_count > NFS_READ_DECR) {
127 readargs.r_count /= 2;
128 nfs_readsize /= 2;
129 dprintf("NFS read size now %d.\n",
130 nfs_readsize);
131 timeout.tv_sec = NFS_REXMIT_MIN;
132 framing_errs = 0;
133 } else {
134 if (timeout.tv_sec < NFS_REXMIT_MAX)
135 timeout.tv_sec++;
136 else
137 timeout.tv_sec = 0;
140 } while (status == RPC_TIMEDOUT);
142 if (status != RPC_SUCCESS)
143 return (-1);
145 if (readres.r_status != NFS4_OK) {
146 nfs4_error(readres.r_status);
147 return (-1);
150 readcnt = readres.r_data_len;
152 if (readres.r_eof == TRUE)
153 done = TRUE;
155 if (readcnt < readargs.r_count) {
156 #ifdef NFS_OPS_DEBUG
157 if ((boothowto & DBFLAGS) == DBFLAGS)
158 printf("nfs4read: partial read %d instead "
159 "of %d\n", readcnt, readargs.count);
160 #endif
161 done = TRUE;
164 count += readcnt;
165 filep->offset += readcnt;
166 buf_offset += readcnt;
167 readargs.r_offset += readcnt;
168 if ((blks_read++ & 0x3) == 0)
169 printf("%c\b", ind[pos++ & 3]);
170 } while (count < size && !done);
172 return (count);
176 static vtype_t nf4_to_vt[] = {
177 VBAD, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
181 nfs4getattr(struct nfs_file *nfp, struct vattr *vap)
183 enum clnt_stat status;
184 attr4_bitmap1_t bitmap1;
185 attr4_bitmap2_t bitmap2;
186 getattr4arg_t getattrargs;
187 getattr4res_t getattrres;
188 b_fattr4_t *bfattr4;
189 utf8string str;
190 char tagname[] = "inetboot getattr";
192 bzero(&getattrres, sizeof (getattrres));
194 * Putfh
196 str.utf8string_len = sizeof (tagname) - 1;
197 str.utf8string_val = tagname;
199 if (nfp->fh.fh4.len > 0)
200 compound_init(&getattrargs.ga_arg, &str, 0, 2, &nfp->fh.fh4);
201 else
202 compound_init(&getattrargs.ga_arg, &str, 0, 2, NULL);
205 * getattr
207 getattrargs.ga_opgetattr = OP_GETATTR;
209 * Set up the attribute bitmap. We pretty much need everything
210 * except for the filehandle and supported attrs.
212 bitmap1.word = 0;
213 bitmap1.bm_fattr4_type = 1;
214 bitmap1.bm_fattr4_size = 1;
215 bitmap1.bm_fattr4_fileid = 1;
216 bitmap2.word = 0;
217 bitmap2.bm_fattr4_mode = 1;
218 bitmap2.bm_fattr4_time_access = 1;
219 bitmap2.bm_fattr4_time_metadata = 1;
220 bitmap2.bm_fattr4_time_modify = 1;
222 getattrargs.ga_attr_req.b_bitmap_len = NFS4_MAX_BITWORDS;
223 getattrargs.ga_attr_req.b_bitmap_val[0] = bitmap1.word;
224 getattrargs.ga_attr_req.b_bitmap_val[1] = bitmap2.word;
226 status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND, xdr_getattr4_args,
227 (caddr_t)&getattrargs, xdr_getattr4_res,
228 (caddr_t)&getattrres, zero_timeout);
230 if (status != RPC_SUCCESS) {
231 dprintf("nfs4getattr: RPC error %d\n", status);
232 return (-1);
235 if (getattrres.gr_attr_status != NFS4_OK) {
236 nfs4_error(getattrres.gr_attr_status);
237 return (getattrres.gr_attr_status);
240 bfattr4 = &getattrres.gr_attrs;
241 if (vap->va_mask & VATTR_TYPE) {
242 if (bfattr4->b_fattr4_type < NF4REG ||
243 bfattr4->b_fattr4_type > NF4FIFO)
244 vap->va_type = VBAD;
245 else
246 vap->va_type = nf4_to_vt[bfattr4->b_fattr4_type];
248 if (vap->va_mask & VATTR_MODE)
249 vap->va_mode = (mode_t)bfattr4->b_fattr4_mode;
250 if (vap->va_mask & VATTR_SIZE)
251 vap->va_size = (uoff_t)bfattr4->b_fattr4_size;
252 if (vap->va_mask & VATTR_NODEID)
253 vap->va_nodeid = (uint64_t)bfattr4->b_fattr4_fileid;
255 * XXX - may need to do something more here.
257 if (vap->va_mask & VATTR_ATIME) {
258 vap->va_atime.tv_sec = bfattr4->b_fattr4_time_access.seconds;
259 vap->va_atime.tv_nsec = bfattr4->b_fattr4_time_access.nseconds;
261 if (vap->va_mask & VATTR_CTIME) {
262 vap->va_ctime.tv_sec = bfattr4->b_fattr4_time_metadata.seconds;
263 vap->va_ctime.tv_nsec =
264 bfattr4->b_fattr4_time_metadata.nseconds;
266 if (vap->va_mask & VATTR_MTIME) {
267 vap->va_mtime.tv_sec = bfattr4->b_fattr4_time_modify.seconds;
268 vap->va_mtime.tv_nsec = bfattr4->b_fattr4_time_modify.nseconds;
271 return (NFS4_OK);
275 * Display nfs error messages.
277 /*ARGSUSED*/
278 void
279 nfs4_error(enum nfsstat4 status)
281 if (!(boothowto & RB_DEBUG))
282 return;
284 switch (status) {
285 case NFS4_OK:
286 printf("NFS: No error.\n");
287 break;
288 case NFS4ERR_PERM:
289 printf("NFS: Not owner.\n");
290 break;
291 case NFS4ERR_NOENT:
292 #ifdef NFS_OPS_DEBUG
293 printf("NFS: No such file or directory.\n");
294 #endif /* NFS_OPS_DEBUG */
295 break;
296 case NFS4ERR_IO:
297 printf("NFS: IO ERROR occurred on NFS server.\n");
298 break;
299 case NFS4ERR_NXIO:
300 printf("NFS: No such device or address.\n");
301 break;
302 case NFS4ERR_ACCESS:
303 printf("NFS: Permission denied.\n");
304 break;
305 case NFS4ERR_EXIST:
306 printf("NFS: File exists.\n");
307 break;
308 case NFS4ERR_XDEV:
309 printf("NFS: Cross device hard link.\n");
310 break;
311 case NFS4ERR_NOTDIR:
312 printf("NFS: Not a directory.\n");
313 break;
314 case NFS4ERR_ISDIR:
315 printf("NFS: Is a directory.\n");
316 break;
317 case NFS4ERR_INVAL:
318 printf("NFS: Invalid argument.\n");
319 break;
320 case NFS4ERR_FBIG:
321 printf("NFS: File too large.\n");
322 break;
323 case NFS4ERR_NOSPC:
324 printf("NFS: No space left on device.\n");
325 break;
326 case NFS4ERR_ROFS:
327 printf("NFS: Read-only filesystem.\n");
328 break;
329 case NFS4ERR_MLINK:
330 printf("NFS: Too many hard links.\n");
331 break;
332 case NFS4ERR_NAMETOOLONG:
333 printf("NFS: File name too long.\n");
334 break;
335 case NFS4ERR_NOTEMPTY:
336 printf("NFS: Directory not empty.\n");
337 break;
338 case NFS4ERR_DQUOT:
339 printf("NFS: Disk quota exceeded.\n");
340 break;
341 case NFS4ERR_STALE:
342 printf("NFS: Stale file handle.\n");
343 break;
344 case NFS4ERR_BADHANDLE:
345 printf("NFS: Illegal NFS file handle.\n");
346 break;
347 case NFS4ERR_BAD_COOKIE:
348 printf("NFS: Stale Cookie.\n");
349 break;
350 case NFS4ERR_NOTSUPP:
351 printf("NFS: Operation is not supported.\n");
352 break;
353 case NFS4ERR_TOOSMALL:
354 printf("NFS: Buffer too small.\n");
355 break;
356 case NFS4ERR_SERVERFAULT:
357 printf("NFS: Server fault.\n");
358 break;
359 case NFS4ERR_BADTYPE:
360 printf("NFS: Unsupported object type.\n");
361 break;
362 case NFS4ERR_BAD_STATEID:
363 printf("NFS: Bad stateid\n");
364 break;
365 case NFS4ERR_BAD_SEQID:
366 printf("NFS: Bad seqid\n");
367 break;
368 default:
369 printf("NFS: unknown error.\n");
370 break;
375 * lookup one component. for multicomponent lookup use a driver like lookup().
377 struct nfs_file *
378 nfs4lookup(struct nfs_file *dir, char *name, int *nstat)
380 static struct nfs_file cd;
381 attr4_bitmap1_t bitmap1;
382 lookup4arg_t lookupargs;
383 lookup4res_t lookupres;
384 enum clnt_stat status;
385 utf8string str;
386 char tagname[] = "inetboot lookup";
389 * NFSv4 uses a special LOOKUPP op
390 * for looking up the parent directory.
392 if (strcmp(name, "..") == 0)
393 return (nfs4lookupp(dir, nstat, NULL));
395 *nstat = (int)NFS4_OK;
397 bzero(&lookupres, sizeof (lookupres));
400 * Check if we have a filehandle and initialize the compound
401 * with putfh or putrootfh appropriately.
403 str.utf8string_len = sizeof (tagname) - 1;
404 str.utf8string_val = tagname;
406 if (dir->fh.fh4.len > 0)
407 compound_init(&lookupargs.la_arg, &str, 0, 3, &dir->fh.fh4);
408 else
409 compound_init(&lookupargs.la_arg, &str, 0, 3, NULL);
412 * lookup
414 lookupargs.la_oplookup = OP_LOOKUP;
416 * convert the pathname from char * to utf8string
418 lookupargs.la_pathname.utf8string_len = strlen(name);
419 lookupargs.la_pathname.utf8string_val =
420 bkmem_alloc(lookupargs.la_pathname.utf8string_len);
421 if (lookupargs.la_pathname.utf8string_val == NULL) {
422 dprintf("nfs4lookup: bkmem_alloc failed\n");
423 return (NULL);
425 bcopy(name, lookupargs.la_pathname.utf8string_val,
426 lookupargs.la_pathname.utf8string_len);
429 * Setup the attr bitmap. All we need is the type and filehandle info
431 lookupargs.la_opgetattr = OP_GETATTR;
432 bitmap1.word = 0;
433 bitmap1.bm_fattr4_type = 1;
434 bitmap1.bm_fattr4_filehandle = 1;
435 lookupargs.la_attr_req.b_bitmap_len = 1;
436 lookupargs.la_attr_req.b_bitmap_val[0] = bitmap1.word;
437 lookupargs.la_attr_req.b_bitmap_val[1] = 0;
439 status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND, xdr_lookup4_args,
440 (caddr_t)&lookupargs, xdr_lookup4_res,
441 (caddr_t)&lookupres, zero_timeout);
443 if (status != RPC_SUCCESS) {
444 dprintf("nfs4lookup: RPC error. status %d\n", status);
445 return (NULL);
448 if (lookupres.lr_lookup_status != NFS4_OK) {
449 #ifdef DEBUG
450 dprintf("nfs4lookup: lookup status = %d\n",
451 lookupres.lr_lookup_status);
452 #endif
453 nfs4_error(lookupres.lr_lookup_status);
454 *nstat = (int)lookupres.lr_lookup_status;
455 if (lookupargs.la_pathname.utf8string_val != NULL)
456 bkmem_free(lookupargs.la_pathname.utf8string_val,
457 lookupargs.la_pathname.utf8string_len);
458 return (NULL);
461 if (lookupres.lr_attr_status != NFS4_OK) {
462 #ifdef DEBUG
463 dprintf("nfs4lookup: getattr status = %d\n",
464 lookupres.lr_attr_status);
465 #endif
466 nfs4_error(lookupres.lr_attr_status);
467 *nstat = (int)lookupres.lr_attr_status;
468 if (lookupargs.la_pathname.utf8string_val != NULL)
469 bkmem_free(lookupargs.la_pathname.utf8string_val,
470 lookupargs.la_pathname.utf8string_len);
471 return (NULL);
475 * We have all the information we need to update the file pointer
477 bzero((caddr_t)&cd, sizeof (struct nfs_file));
478 cd.version = NFS_V4;
479 cd.ftype.type4 = lookupres.lr_attrs.b_fattr4_type;
480 cd.fh.fh4.len = lookupres.lr_attrs.b_fattr4_filehandle.len;
481 bcopy(lookupres.lr_attrs.b_fattr4_filehandle.data, cd.fh.fh4.data,
482 cd.fh.fh4.len);
485 * Free the arg string
487 if (lookupargs.la_pathname.utf8string_val != NULL)
488 bkmem_free(lookupargs.la_pathname.utf8string_val,
489 lookupargs.la_pathname.utf8string_len);
491 return (&cd);
495 * lookup parent directory.
497 struct nfs_file *
498 nfs4lookupp(struct nfs_file *dir, int *nstat, uint64_t *fileid)
500 static struct nfs_file cd;
501 attr4_bitmap1_t bitmap1;
502 lookupp4arg_t lookuppargs;
503 lookup4res_t lookupres;
504 enum clnt_stat status;
505 utf8string str;
506 char tagname[] = "inetboot lookupp";
508 *nstat = (int)NFS4_OK;
510 bzero(&lookupres, sizeof (lookupres));
513 * Check if we have a filehandle and initialize the compound
514 * with putfh or putrootfh appropriately.
516 str.utf8string_len = sizeof (tagname) - 1;
517 str.utf8string_val = tagname;
519 if (dir->fh.fh4.len > 0)
520 compound_init(&lookuppargs.la_arg, &str, 0, 3, &dir->fh.fh4);
521 else
522 compound_init(&lookuppargs.la_arg, &str, 0, 3, NULL);
525 * lookupp
527 lookuppargs.la_oplookupp = OP_LOOKUPP;
529 * Setup the attr bitmap. Normally, all we need is the type and
530 * filehandle info, but getdents might require the fileid of the
531 * parent.
533 lookuppargs.la_opgetattr = OP_GETATTR;
534 bitmap1.word = 0;
535 bitmap1.bm_fattr4_type = 1;
536 bitmap1.bm_fattr4_filehandle = 1;
537 if (fileid != NULL)
538 bitmap1.bm_fattr4_fileid = 1;
539 lookuppargs.la_attr_req.b_bitmap_len = 1;
540 lookuppargs.la_attr_req.b_bitmap_val[0] = bitmap1.word;
541 lookuppargs.la_attr_req.b_bitmap_val[1] = 0;
543 status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND, xdr_lookupp4_args,
544 (caddr_t)&lookuppargs, xdr_lookup4_res,
545 (caddr_t)&lookupres, zero_timeout);
547 if (status != RPC_SUCCESS) {
548 dprintf("nfs4lookupp: RPC error. status %d\n", status);
549 return (NULL);
552 if (lookupres.lr_lookup_status != NFS4_OK) {
553 #ifdef DEBUG
554 dprintf("nfs4lookupp: lookupp status = %d\n",
555 lookupres.lr_lookup_status);
556 #endif
557 nfs4_error(lookupres.lr_lookup_status);
558 *nstat = (int)lookupres.lr_lookup_status;
559 return (NULL);
562 if (lookupres.lr_attr_status != NFS4_OK) {
563 #ifdef DEBUG
564 dprintf("nfs4lookupp: getattr status = %d\n",
565 lookupres.lr_attr_status);
566 #endif
567 nfs4_error(lookupres.lr_attr_status);
568 *nstat = (int)lookupres.lr_attr_status;
569 return (NULL);
573 * We have all the information we need to update the file pointer
575 bzero((caddr_t)&cd, sizeof (struct nfs_file));
576 cd.version = NFS_V4;
577 cd.ftype.type4 = lookupres.lr_attrs.b_fattr4_type;
578 cd.fh.fh4.len = lookupres.lr_attrs.b_fattr4_filehandle.len;
579 bcopy(lookupres.lr_attrs.b_fattr4_filehandle.data, cd.fh.fh4.data,
580 cd.fh.fh4.len);
583 * Fill in the fileid if the user passed in one
585 if (fileid != NULL)
586 *fileid = lookupres.lr_attrs.b_fattr4_fileid;
588 return (&cd);
592 * Gets symbolic link into pathname.
595 nfs4getsymlink(struct nfs_file *cfile, char **path)
597 enum clnt_stat status;
598 readlink4arg_t readlinkargs;
599 readlink4res_t readlinkres;
600 static char symlink_path[NFS_MAXPATHLEN];
601 int spathlen;
602 utf8string str;
603 char tagname[] = "inetboot getsymlink";
604 int error = NFS4_OK;
606 bzero(&readlinkres, sizeof (readlinkres));
609 * readlink
611 str.utf8string_len = sizeof (tagname) - 1;
612 str.utf8string_val = tagname;
614 if (cfile->fh.fh4.len > 0)
615 compound_init(&readlinkargs.rl_arg, &str, 0, 2,
616 &cfile->fh.fh4);
617 else
618 compound_init(&readlinkargs.rl_arg, &str, 0, 2, NULL);
620 readlinkargs.rl_opreadlink = OP_READLINK;
621 status = CLNT_CALL(root_CLIENT, NFSPROC4_COMPOUND, xdr_readlink4_args,
622 (caddr_t)&readlinkargs, xdr_readlink4_res,
623 (caddr_t)&readlinkres, zero_timeout);
625 if (status != RPC_SUCCESS) {
626 dprintf("nfs4getsymlink: RPC readlink error %d\n", status);
627 error = -1;
628 goto out;
631 if (readlinkres.rl_status != NFS4_OK) {
632 nfs4_error(readlinkres.rl_status);
633 error = readlinkres.rl_status;
634 goto out;
638 * Convert the utf8string to a normal character string
640 spathlen = readlinkres.rl_link.utf8string_len;
641 if (spathlen <= 0 || readlinkres.rl_link.utf8string_val == NULL) {
642 *path = NULL;
643 error = readlinkres.rl_status;
644 goto out;
647 bcopy(readlinkres.rl_link.utf8string_val, symlink_path, spathlen);
648 symlink_path[spathlen] = '\0';
649 *path = symlink_path;
651 out:
653 * Free the results
655 if (readlinkres.rl_link.utf8string_val != NULL)
656 bkmem_free(readlinkres.rl_link.utf8string_val, spathlen);
658 return (error);
662 * Should just forget about the tag, but will leave in support for the time
663 * being.
665 void
666 compound_init(b_compound_t *cp, utf8string *str, uint_t mvers, uint_t arglen,
667 struct nfs_bfh4 *pfh)
669 if (str == NULL || str->utf8string_len == 0) {
670 cp->ca_tag.utf8string_len = 0;
671 cp->ca_tag.utf8string_val = NULL;
672 } else {
673 cp->ca_tag.utf8string_len = str->utf8string_len;
674 cp->ca_tag.utf8string_val = str->utf8string_val;
676 cp->ca_minorversion = mvers;
677 cp->ca_argarray_len = arglen;
678 if (pfh == NULL) {
679 cp->ca_isputrootfh = TRUE;
680 cp->ca_opputfh.pf_opnum = OP_PUTROOTFH;
681 } else {
682 cp->ca_isputrootfh = FALSE;
683 cp->ca_opputfh.pf_opnum = OP_PUTFH;
684 cp->ca_opputfh.pf_filehandle.len = pfh->len;
685 bcopy(pfh->data, cp->ca_opputfh.pf_filehandle.data, pfh->len);