blob: e4b6253de599803678be406bf9211c9defc098d6 [file] [log] [blame]
rscd3df3082003-12-06 18:08:52 +00001/* Copyright (C) 2003 Russ Cox, Massachusetts Institute of Technology */
2/* See COPYRIGHT */
3
4#include <u.h>
5#include <libc.h>
6#include <fcall.h>
7#include <fs.h>
8#include "fsimpl.h"
9
rsc5a8e63b2004-02-29 22:10:26 +000010static long
11_fspwrite(Fid *fid, void *buf, long n, vlong offset)
rscd3df3082003-12-06 18:08:52 +000012{
13 Fcall tx, rx;
14 void *freep;
15
rscceb04772003-12-09 06:06:07 +000016 tx.type = Twrite;
17 tx.fid = fid->fid;
rscd3df3082003-12-06 18:08:52 +000018 if(offset == -1){
rscceb04772003-12-09 06:06:07 +000019 qlock(&fid->lk);
20 tx.offset = fid->offset;
21 qunlock(&fid->lk);
rscd3df3082003-12-06 18:08:52 +000022 }else
23 tx.offset = offset;
24 tx.count = n;
25 tx.data = buf;
26
rsc15680d52004-03-05 05:53:11 +000027 if(fsrpc(fid->fs, &tx, &rx, &freep) < 0)
28 return -1;
rscd3df3082003-12-06 18:08:52 +000029 if(rx.type == Rerror){
rscd3df3082003-12-06 18:08:52 +000030 werrstr("%s", rx.ename);
31 free(freep);
32 return -1;
33 }
rscceb04772003-12-09 06:06:07 +000034 if(offset == -1 && rx.count){
35 qlock(&fid->lk);
36 fid->offset += rx.count;
37 qunlock(&fid->lk);
38 }
rscd3df3082003-12-06 18:08:52 +000039 free(freep);
40 return rx.count;
41}
42
43long
rsc5a8e63b2004-02-29 22:10:26 +000044fspwrite(Fid *fid, void *buf, long n, vlong offset)
45{
rsc493f3d02004-10-22 17:14:17 +000046 long tot, want, got, first;
rsc5a8e63b2004-02-29 22:10:26 +000047 uint msize;
48
49 msize = fid->fs->msize - IOHDRSZ;
50 tot = 0;
rsc493f3d02004-10-22 17:14:17 +000051 first = 1;
52 while(tot < n || first){
rsc5a8e63b2004-02-29 22:10:26 +000053 want = n - tot;
54 if(want > msize)
55 want = msize;
56 got = _fspwrite(fid, buf, want, offset);
rsc493f3d02004-10-22 17:14:17 +000057 first = 0;
rsc5a8e63b2004-02-29 22:10:26 +000058 if(got < 0){
59 if(tot == 0)
60 return got;
61 break;
62 }
63 tot += got;
64 if(offset != -1)
65 offset += got;
66 }
67 return tot;
68}
69
70long
rscceb04772003-12-09 06:06:07 +000071fswrite(Fid *fid, void *buf, long n)
rscd3df3082003-12-06 18:08:52 +000072{
rscceb04772003-12-09 06:06:07 +000073 return fspwrite(fid, buf, n, -1);
rscd3df3082003-12-06 18:08:52 +000074}