]> git.lizzy.rs Git - plan9front.git/blob - sys/src/lib9p/file.c
lib9p: fix .. walk crash in deleted directory (thanks BurnZeZ)
[plan9front.git] / sys / src / lib9p / file.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include <thread.h>
6 #include <9p.h>
7
8 /*
9  * To avoid deadlock, the following rules must be followed.
10  * Always lock child then parent, never parent then child.
11  * If holding the free file lock, do not lock any Files.
12  */
13 struct Filelist
14 {
15         File *f;
16         Filelist *link;
17 };
18
19 struct Readdir
20 {
21         File *dir;
22         Filelist *fl;
23 };
24
25 static QLock filelk;
26 static File *freefilelist;
27
28 static File*
29 allocfile(void)
30 {
31         int i, a;
32         File *f;
33         enum { N = 16 };
34
35         qlock(&filelk);
36         if(freefilelist == nil){
37                 f = emalloc9p(N*sizeof(*f));
38                 for(i=0; i<N-1; i++)
39                         f[i].aux = &f[i+1];
40                 f[N-1].aux = nil;
41                 f[0].allocd = 1;
42                 freefilelist = f;
43         }
44
45         f = freefilelist;
46         freefilelist = f->aux;
47         qunlock(&filelk);
48
49         a = f->allocd;
50         memset(f, 0, sizeof *f);
51         f->allocd = a;
52         return f;
53 }
54
55 static void
56 freefile(File *f)
57 {
58         Filelist *fl, *flnext;
59
60         for(fl=f->filelist; fl; fl=flnext){
61                 flnext = fl->link;
62                 assert(fl->f == nil);
63                 free(fl);
64         }
65
66         free(f->name);
67         free(f->uid);
68         free(f->gid);
69         free(f->muid);
70         qlock(&filelk);
71         assert(f->ref == 0);
72         f->aux = freefilelist;
73         freefilelist = f;
74         qunlock(&filelk);
75 }
76
77 static void
78 cleanfilelist(File *f)
79 {
80         Filelist **l;
81         Filelist *fl;
82         
83         /*
84          * can't delete filelist structures while there
85          * are open readers of this directory, because
86          * they might have references to the structures.
87          * instead, just leave the empty refs in the list
88          * until there is no activity and then clean up.
89          */
90         if(f->readers.ref != 0)
91                 return;
92         if(f->nxchild == 0)
93                 return;
94
95         /*
96          * no dir readers, file is locked, and
97          * there are empty entries in the file list.
98          * clean them out.
99          */
100         for(l=&f->filelist; fl=*l; ){
101                 if(fl->f == nil){
102                         *l = (*l)->link;
103                         free(fl);
104                 }else
105                         l = &(*l)->link;
106         }
107         f->nxchild = 0;
108 }
109
110 void
111 closefile(File *f)
112 {
113         if(decref(f) == 0){
114                 f->tree->destroy(f);
115                 freefile(f);
116         }
117 }
118
119 static void
120 nop(File*)
121 {
122 }
123
124 int
125 removefile(File *f)
126 {
127         File *fp;
128         Filelist *fl;
129         
130         fp = f->parent;
131         if(fp == nil){
132                 werrstr("no parent");
133                 closefile(f);
134                 return -1;
135         }
136
137         if(fp == f){
138                 werrstr("cannot remove root");
139                 closefile(f);
140                 return -1;
141         }
142
143         wlock(f);
144         wlock(fp);
145         if(f->nchild != 0){
146                 werrstr("has children");
147                 wunlock(fp);
148                 wunlock(f);
149                 closefile(f);
150                 return -1;
151         }
152
153         if(f->parent != fp){
154                 werrstr("parent changed underfoot");
155                 wunlock(fp);
156                 wunlock(f);
157                 closefile(f);
158                 return -1;
159         }
160
161         for(fl=fp->filelist; fl; fl=fl->link)
162                 if(fl->f == f)
163                         break;
164         assert(fl != nil && fl->f == f);
165
166         fl->f = nil;
167         fp->nchild--;
168         fp->nxchild++;
169         f->parent = nil;
170         wunlock(f);
171
172         cleanfilelist(fp);
173         wunlock(fp);
174
175         closefile(fp);  /* reference from child */
176         closefile(f);   /* reference from tree */
177         closefile(f);
178         return 0;
179 }
180
181 File*
182 createfile(File *fp, char *name, char *uid, ulong perm, void *aux)
183 {
184         File *f;
185         Filelist **l, *fl;
186         Tree *t;
187
188         if((fp->qid.type&QTDIR) == 0){
189                 werrstr("create in non-directory");
190                 return nil;
191         }
192
193         wlock(fp);
194         /*
195          * We might encounter blank spots along the
196          * way due to deleted files that have not yet
197          * been flushed from the file list.  Don't reuse
198          * those - some apps (e.g., omero) depend on
199          * the file order reflecting creation order. 
200          * Always create at the end of the list.
201          */
202         for(l=&fp->filelist; fl=*l; l=&fl->link){
203                 if(fl->f && strcmp(fl->f->name, name) == 0){
204                         wunlock(fp);
205                         werrstr("file already exists");
206                         return nil;
207                 }
208         }
209         
210         fl = emalloc9p(sizeof *fl);
211         *l = fl;
212
213         f = allocfile();
214         f->name = estrdup9p(name);
215         f->uid = estrdup9p(uid ? uid : fp->uid);
216         f->gid = estrdup9p(fp->gid);
217         f->muid = estrdup9p(uid ? uid : "unknown");
218         f->aux = aux;
219         f->mode = perm;
220
221         t = fp->tree;
222         lock(&t->genlock);
223         f->qid.path = t->qidgen++;
224         unlock(&t->genlock);
225         if(perm & DMDIR)
226                 f->qid.type |= QTDIR;
227         if(perm & DMAPPEND)
228                 f->qid.type |= QTAPPEND;
229         if(perm & DMEXCL)
230                 f->qid.type |= QTEXCL;
231
232         f->mode = perm;
233         f->atime = f->mtime = time(0);
234         f->length = 0;
235         f->parent = fp;
236         incref(fp);
237         f->tree = fp->tree;
238
239         incref(f);      /* being returned */
240         incref(f);      /* for the tree */
241         fl->f = f;
242         fp->nchild++;
243         wunlock(fp);
244
245         return f;
246 }
247
248 static File*
249 walkfile1(File *dir, char *elem)
250 {
251         File *fp;
252         Filelist *fl;
253
254         rlock(dir);
255         if(strcmp(elem, "..") == 0){
256                 fp = dir->parent;
257                 if(fp != nil)
258                         incref(fp);
259                 runlock(dir);
260                 closefile(dir);
261                 return fp;
262         }
263
264         fp = nil;
265         for(fl=dir->filelist; fl; fl=fl->link)
266                 if(fl->f && strcmp(fl->f->name, elem)==0){
267                         fp = fl->f;
268                         incref(fp);
269                         break;
270                 }
271
272         runlock(dir);
273         closefile(dir);
274         return fp;
275 }
276
277 File*
278 walkfile(File *f, char *path)
279 {
280         char *os, *s, *nexts;
281
282         if(strchr(path, '/') == nil)
283                 return walkfile1(f, path);      /* avoid malloc */
284
285         os = s = estrdup9p(path);
286         for(; *s; s=nexts){
287                 if(nexts = strchr(s, '/'))
288                         *nexts++ = '\0';
289                 else
290                         nexts = s+strlen(s);
291                 f = walkfile1(f, s);
292                 if(f == nil)
293                         break;
294         }
295         free(os);
296         return f;
297 }
298                         
299 Tree*
300 alloctree(char *uid, char *gid, ulong mode, void (*destroy)(File*))
301 {
302         char *muid;
303         Tree *t;
304         File *f;
305
306         t = emalloc9p(sizeof *t);
307         f = allocfile();
308         f->name = estrdup9p("/");
309         if(uid == nil){
310                 uid = getuser();
311                 if(uid == nil)
312                         uid = "none";
313         }
314         uid = estrdup9p(uid);
315
316         if(gid == nil)
317                 gid = estrdup9p(uid);
318         else
319                 gid = estrdup9p(gid);
320
321         muid = estrdup9p(uid);
322
323         f->qid = (Qid){0, 0, QTDIR};
324         f->length = 0;
325         f->atime = f->mtime = time(0);
326         f->mode = DMDIR | mode;
327         f->tree = t;
328         f->parent = f;
329         f->uid = uid;
330         f->gid = gid;
331         f->muid = muid;
332
333         incref(f);
334         t->root = f;
335         t->qidgen = 0;
336         t->dirqidgen = 1;
337         if(destroy == nil)
338                 destroy = nop;
339         t->destroy = destroy;
340
341         return t;
342 }
343
344 static void
345 _freefiles(File *f)
346 {
347         Filelist *fl, *flnext;
348
349         for(fl=f->filelist; fl; fl=flnext){
350                 flnext = fl->link;
351                 _freefiles(fl->f);
352                 free(fl);
353         }
354
355         f->tree->destroy(f);
356         freefile(f);
357 }
358
359 void
360 freetree(Tree *t)
361 {
362         _freefiles(t->root);
363         free(t);
364 }
365
366 Readdir*
367 opendirfile(File *dir)
368 {
369         Readdir *r;
370
371         rlock(dir);
372         if((dir->mode & DMDIR)==0){
373                 runlock(dir);
374                 return nil;
375         }
376         r = emalloc9p(sizeof(*r));
377
378         /*
379          * This reference won't go away while we're 
380          * using it because file list entries are not freed
381          * until the directory is removed and all refs to
382          * it (our fid is one!) have gone away.
383          */
384         r->fl = dir->filelist;
385         r->dir = dir;
386         incref(&dir->readers);
387         runlock(dir);
388         return r;
389 }
390
391 long
392 readdirfile(Readdir *r, uchar *buf, long n)
393 {
394         long x, m;
395         Filelist *fl;
396
397         for(fl=r->fl, m=0; fl && m+2<=n; fl=fl->link, m+=x){
398                 if(fl->f == nil)
399                         x = 0;
400                 else if((x=convD2M(fl->f, buf+m, n-m)) <= BIT16SZ)
401                         break;
402         }
403         r->fl = fl;
404         return m;
405 }
406
407 void
408 closedirfile(Readdir *r)
409 {
410         if(decref(&r->dir->readers) == 0){
411                 wlock(r->dir);
412                 cleanfilelist(r->dir);
413                 wunlock(r->dir);
414         }
415         free(r);
416 }