1 /* $NetBSD: smbfs_io.c,v 1.32 2009/03/18 16:00:21 cegger Exp $ */
4 * Copyright (c) 2000-2001, Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * FreeBSD: src/sys/fs/smbfs/smbfs_io.c,v 1.7 2001/12/02 08:56:58 bp Exp
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: smbfs_io.c,v 1.32 2009/03/18 16:00:21 cegger Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
44 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
48 #include <sys/mount.h>
49 #include <sys/malloc.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/dirent.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/vmmeter.h>
56 #include <sys/kauth.h>
60 #if __FreeBSD_version < 400000
61 #include <vm/vm_prot.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_pager.h>
67 #include <vm/vnode_pager.h>
68 #else /* __NetBSD__ */
70 #include <uvm/uvm_extern.h>
71 #endif /* __NetBSD__ */
74 #include <sys/ioccom.h>
76 #include <netsmb/smb.h>
77 #include <netsmb/smb_conn.h>
78 #include <netsmb/smb_subr.h>
80 #include <fs/smbfs/smbfs.h>
81 #include <fs/smbfs/smbfs_node.h>
82 #include <fs/smbfs/smbfs_subr.h>
84 /*#define SMBFS_RWGENERIC*/
86 #define DE_SIZE (sizeof(struct dirent))
89 smbfs_readvdir(struct vnode
*vp
, struct uio
*uio
, kauth_cred_t cred
)
92 struct smb_cred scred
;
93 struct smbfs_fctx
*ctx
;
94 struct smbnode
*np
= VTOSMB(vp
);
95 int error
= 0/*, *eofflag = ap->a_eofflag*/;
98 KASSERT(vp
->v_type
== VDIR
);
100 if (uio
->uio_resid
< DE_SIZE
|| uio
->uio_offset
< 0)
103 de
= malloc(DE_SIZE
, M_SMBFSDATA
, M_WAITOK
| M_ZERO
);
105 SMBVDEBUG("dirname='%.*s'\n", (int) np
->n_nmlen
, np
->n_name
);
106 smb_makescred(&scred
, curlwp
, cred
);
107 offset
= uio
->uio_offset
/ DE_SIZE
; /* offset in the directory */
108 limit
= uio
->uio_resid
/ DE_SIZE
;
112 memset(de
, 0, DE_SIZE
);
113 de
->d_fileno
= np
->n_ino
;
114 de
->d_reclen
= DE_SIZE
;
117 strncpy(de
->d_name
, ".", 2);
118 error
= uiomove(de
, DE_SIZE
, uio
);
125 if (limit
> 0 && offset
< 2) {
126 memset(de
, 0, DE_SIZE
);
127 de
->d_fileno
= (np
->n_parent
? VTOSMB(np
->n_parent
)->n_ino
: 2);
128 de
->d_reclen
= DE_SIZE
;
131 strncpy(de
->d_name
, "..", 3);
132 error
= uiomove(de
, DE_SIZE
, uio
);
142 if (offset
!= np
->n_dirofs
|| np
->n_dirseq
== NULL
) {
143 SMBVDEBUG("Reopening search %ld:%ld\n", offset
, np
->n_dirofs
);
145 smbfs_findclose(np
->n_dirseq
, &scred
);
149 error
= smbfs_findopen(np
, "*", 1,
150 SMB_FA_SYSTEM
| SMB_FA_HIDDEN
| SMB_FA_DIR
,
153 SMBVDEBUG("can not open search, error = %d", error
);
160 /* skip entries before offset */
161 while (np
->n_dirofs
< offset
) {
162 error
= smbfs_findnext(ctx
, offset
- np
->n_dirofs
++, &scred
);
164 smbfs_findclose(np
->n_dirseq
, &scred
);
166 error
= (error
== ENOENT
) ? 0 : error
;
171 for (; limit
; limit
--, offset
++) {
172 error
= smbfs_findnext(ctx
, limit
, &scred
);
179 memset(de
, 0, DE_SIZE
);
180 de
->d_reclen
= DE_SIZE
;
181 de
->d_fileno
= ctx
->f_attr
.fa_ino
;
182 de
->d_type
= (ctx
->f_attr
.fa_attr
& SMB_FA_DIR
) ?
184 de
->d_namlen
= ctx
->f_nmlen
;
185 memcpy(de
->d_name
, ctx
->f_name
, de
->d_namlen
);
186 de
->d_name
[de
->d_namlen
] = '\0';
187 error
= uiomove(de
, DE_SIZE
, uio
);
193 free(de
, M_SMBFSDATA
);
198 smbfs_readvnode(struct vnode
*vp
, struct uio
*uiop
, kauth_cred_t cred
)
200 struct smbmount
*smp
= VFSTOSMBFS(vp
->v_mount
);
201 struct smbnode
*np
= VTOSMB(vp
);
202 struct lwp
*l
= curlwp
;
204 struct smb_cred scred
;
207 KASSERT(vp
->v_type
== VREG
|| vp
->v_type
== VDIR
);
209 if (uiop
->uio_resid
== 0)
211 if (uiop
->uio_offset
< 0)
213 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
215 if (vp
->v_type
== VDIR
) {
216 error
= smbfs_readvdir(vp
, uiop
, cred
);
220 if (np
->n_flag
& NMODIFIED
) {
221 smbfs_attr_cacheremove(vp
);
222 error
= VOP_GETATTR(vp
, &vattr
, cred
);
225 np
->n_mtime
.tv_sec
= vattr
.va_mtime
.tv_sec
;
227 error
= VOP_GETATTR(vp
, &vattr
, cred
);
230 if (np
->n_mtime
.tv_sec
!= vattr
.va_mtime
.tv_sec
) {
231 error
= smbfs_vinvalbuf(vp
, V_SAVE
, cred
, l
, 1);
234 np
->n_mtime
.tv_sec
= vattr
.va_mtime
.tv_sec
;
237 smb_makescred(&scred
, l
, cred
);
238 return smb_read(smp
->sm_share
, np
->n_fid
, uiop
, &scred
);
242 smbfs_writevnode(struct vnode
*vp
, struct uio
*uiop
,
243 kauth_cred_t cred
, int ioflag
)
245 struct smbmount
*smp
= VTOSMBFS(vp
);
246 struct smbnode
*np
= VTOSMB(vp
);
247 struct smb_cred scred
;
248 struct lwp
*l
= curlwp
;
249 struct proc
*p
= l
->l_proc
;
252 size_t resid
= uiop
->uio_resid
;
254 /* vn types other than VREG unsupported */
255 KASSERT(vp
->v_type
== VREG
);
257 SMBVDEBUG("ofs=%lld,resid=%zu\n",
258 (long long int) uiop
->uio_offset
,
260 if (uiop
->uio_offset
< 0)
262 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
264 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
265 if (np
->n_flag
& NMODIFIED
) {
266 smbfs_attr_cacheremove(vp
);
267 error
= smbfs_vinvalbuf(vp
, V_SAVE
, cred
, l
, 1);
271 if (ioflag
& IO_APPEND
) {
274 * File size can be changed by another client
276 smbfs_attr_cacheremove(vp
);
277 error
= VOP_GETATTR(vp
, &vattr
, cred
, td
);
281 uiop
->uio_offset
= np
->n_size
;
284 if (uiop
->uio_resid
== 0)
286 if (p
&& uiop
->uio_offset
+ uiop
->uio_resid
> p
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
287 mutex_enter(proc_lock
);
289 mutex_exit(proc_lock
);
292 smb_makescred(&scred
, l
, cred
);
293 error
= smb_write(smp
->sm_share
, np
->n_fid
, uiop
, &scred
);
294 SMBVDEBUG("after: ofs=%lld,resid=%zu,err=%d\n",
295 (long long int)uiop
->uio_offset
, uiop
->uio_resid
, error
);
297 if (uiop
->uio_offset
> np
->n_size
) {
298 np
->n_size
= uiop
->uio_offset
;
299 uvm_vnp_setsize(vp
, np
->n_size
);
304 if (resid
> uiop
->uio_resid
)
305 VN_KNOTE(vp
, NOTE_WRITE
| (extended
? NOTE_EXTEND
: 0));
310 * Do an I/O operation to/from a cache block.
313 smbfs_doio(struct buf
*bp
, kauth_cred_t cr
, struct lwp
*l
)
315 struct vnode
*vp
= bp
->b_vp
;
316 struct smbmount
*smp
= VFSTOSMBFS(vp
->v_mount
);
317 struct smbnode
*np
= VTOSMB(vp
);
318 struct uio uio
, *uiop
= &uio
;
320 struct smb_cred scred
;
324 uiop
->uio_iovcnt
= 1;
325 UIO_SETUP_SYSSPACE(uiop
);
327 smb_makescred(&scred
, l
, cr
);
329 if (bp
->b_flags
== B_READ
) {
330 io
.iov_len
= uiop
->uio_resid
= bp
->b_bcount
;
331 io
.iov_base
= bp
->b_data
;
332 uiop
->uio_rw
= UIO_READ
;
333 switch (vp
->v_type
) {
335 uiop
->uio_offset
= ((off_t
)bp
->b_blkno
) * DEV_BSIZE
;
336 error
= smb_read(smp
->sm_share
, np
->n_fid
, uiop
, &scred
);
339 if (uiop
->uio_resid
) {
340 int left
= uiop
->uio_resid
;
341 int nread
= bp
->b_bcount
- left
;
343 memset((char *)bp
->b_data
+ nread
, 0, left
);
347 printf("smbfs_doio: type %x unexpected\n",vp
->v_type
);
354 io
.iov_len
= uiop
->uio_resid
= bp
->b_bcount
;
355 uiop
->uio_offset
= ((off_t
)bp
->b_blkno
) << DEV_BSHIFT
;
356 io
.iov_base
= bp
->b_data
;
357 uiop
->uio_rw
= UIO_WRITE
;
358 bp
->b_cflags
|= BC_BUSY
;
359 error
= smb_write(smp
->sm_share
, np
->n_fid
, uiop
, &scred
);
360 bp
->b_cflags
&= ~BC_BUSY
;
363 #ifndef __NetBSD__ /* XXX */
365 * For an interrupted write, the buffer is still valid
366 * and the write hasn't been pushed to the server yet,
367 * so we can't set BIO_ERROR and report the interruption
368 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
369 * is not relevant, so the rpc attempt is essentially
370 * a noop. For the case of a V3 write rpc not being
371 * committed to stable storage, the block is still
372 * dirty and requires either a commit rpc or another
373 * write rpc with iomode == NFSV3WRITE_FILESYNC before
374 * the block is reused. This is indicated by setting
375 * the B_DELWRI and B_NEEDCOMMIT flags.
378 || (!error
&& (bp
->b_flags
& B_NEEDCOMMIT
))) {
382 bp
->b_flags
&= ~(B_INVAL
|B_NOCACHE
);
383 if ((bp
->b_flags
& B_ASYNC
) == 0)
384 bp
->b_flags
|= B_EINTR
;
385 if ((bp
->b_flags
& B_PAGING
) == 0) {
387 bp
->b_flags
&= ~B_DONE
;
389 if ((bp
->b_flags
& B_ASYNC
) == 0)
390 bp
->b_flags
|= B_EINTR
;
394 bp
->b_ioflags
|= BIO_ERROR
;
397 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
399 #endif /* !__NetBSD__ */
401 bp
->b_resid
= uiop
->uio_resid
;
407 * Flush and invalidate all dirty buffers. If another process is already
408 * doing the flush, just wait for completion.
411 smbfs_vinvalbuf(struct vnode
*vp
, int flags
, kauth_cred_t cred
, struct lwp
*l
, int intrflg
)
413 struct smbnode
*np
= VTOSMB(vp
);
414 int error
= 0, slpflag
;
421 while (np
->n_flag
& NFLUSHINPROG
) {
422 np
->n_flag
|= NFLUSHWANT
;
423 error
= tsleep(&np
->n_flag
,
424 (PRIBIO
+ 2) | slpflag
, "smfsvinv", 0);
428 np
->n_flag
|= NFLUSHINPROG
;
430 if ((error
= vinvalbuf(vp
, flags
, cred
, l
, slpflag
, 0)) == 0)
433 if (intrflg
&& (error
== ERESTART
|| error
== EINTR
)) {
434 np
->n_flag
&= ~NFLUSHINPROG
;
435 if (np
->n_flag
& NFLUSHWANT
) {
436 np
->n_flag
&= ~NFLUSHWANT
;
442 np
->n_flag
&= ~(NMODIFIED
| NFLUSHINPROG
);
443 if (np
->n_flag
& NFLUSHWANT
) {
444 np
->n_flag
&= ~NFLUSHWANT
;