Whamcloud - gitweb
LU-864 test: Hostname name doesn't equal NID and use facet_mntpt
[fs/lustre-release.git] / libsysio / src / file.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <stdlib.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/queue.h>
50
51 #include "sysio.h"
52 #include "file.h"
53 #include "inode.h"
54
55 /*
56  * Support for file IO.
57  */
58
59 /*
60  * The open files table and it's size.
61  */
62 static struct file **_sysio_oftab = NULL;
63 static size_t _sysio_oftab_size = 0;
64
65 /*
66  * Create and initialize open file record.
67  */
68 struct file *
69 _sysio_fnew(struct inode *ino, int flags)
70 {
71         struct file *fil;
72
73         fil = malloc(sizeof(struct file));
74         if (!fil)
75                 return NULL;
76
77         _SYSIO_FINIT(fil, ino, flags);
78         F_REF(fil);
79         I_REF(fil->f_ino);
80
81         return fil;
82 }
83
84 /*
85  * Destroy open file record.
86  */
87 void
88 _sysio_fgone(struct file *fil)
89 {
90         int     err;
91
92         assert(!fil->f_ref);
93         assert(fil->f_ino);
94         err = (*fil->f_ino->i_ops.inop_close)(fil->f_ino);
95         assert(!err);
96         I_RELE(fil->f_ino);
97         free(fil);
98 }
99
100 /*
101  * IO operation completion handler.
102  */
103 void
104 _sysio_fcompletio(struct ioctx *ioctx, struct file *fil)
105 {
106         _SYSIO_OFF_T off;
107
108         if (ioctx->ioctx_cc <= 0)
109                 return;
110
111         assert(ioctx->ioctx_ino == fil->f_ino);
112         off = fil->f_pos + ioctx->ioctx_cc;
113         if (fil->f_pos && off <= fil->f_pos)
114                 abort();
115         fil->f_pos = off;
116 }
117
118 /*
119  * Grow (or truncate) the file descriptor table.
120  */
121 static int
122 fd_grow(size_t n)
123 {
124         size_t  count;
125         struct file **noftab, **filp;
126
127         /*
128          * Sanity check the new size.
129          */
130         if ((int )n < 0)
131                 return -EMFILE;
132
133         /*
134          * We never shrink the table.
135          */
136         if (n <= _sysio_oftab_size)
137                 return 0;
138
139         noftab = realloc(_sysio_oftab, n * sizeof(struct file *));
140         if (!noftab)
141                 return -ENOMEM;
142         _sysio_oftab = noftab;
143         count = _sysio_oftab_size;
144         _sysio_oftab_size = n;
145         filp = _sysio_oftab + count;
146         n -= count;
147         while (n--)
148                 *filp++ = NULL;
149         return 0;
150 }
151
152 #ifdef ZERO_SUM_MEMORY
153 void
154 _sysio_fd_shutdown()
155 {
156
157         free(_sysio_oftab);
158         _sysio_oftab_size = 0;
159 }
160 #endif
161
162 /*
163  * Find a free slot in the open files table greater than or equal to the
164  * argument.
165  */
166 static int
167 find_free_fildes(int low)
168 {
169         int     n;
170         int     err;
171         struct file **filp;
172
173         for (n = low, filp = _sysio_oftab + low;
174              n >= 0 && (unsigned )n < _sysio_oftab_size && *filp;
175              n++, filp++)
176                 ;
177         if (n < 0)
178                 return -ENFILE;
179         if ((unsigned )n >= _sysio_oftab_size) {
180                 err = fd_grow((unsigned )n + 1);
181                 if (err)
182                         return err;
183                 filp = &_sysio_oftab[n];
184                 assert(!*filp);
185         }
186
187         return n;
188 }
189
190 /*
191  * Find open file record from file descriptor.
192  */
193 struct file *
194 _sysio_fd_find(int fd)
195 {
196         if (fd < 0 || (unsigned )fd >= _sysio_oftab_size)
197                 return NULL;
198
199         return _sysio_oftab[fd];
200 }
201
202 /*
203  * Close an open descriptor.
204  */
205 int
206 _sysio_fd_close(int fd)
207 {
208         struct file *fil;
209
210         fil = _sysio_fd_find(fd);
211         if (!fil)
212                 return -EBADF;
213
214         _sysio_oftab[fd] = NULL;
215
216         F_RELE(fil);
217
218         return 0;
219 }
220
221 /*
222  * Associate open file record with given file descriptor (if forced), or any
223  * available file descriptor if less than zero, or any available descriptor
224  * greater than or equal to the given one if not forced.
225  */
226 int
227 _sysio_fd_set(struct file *fil, int fd, int force)
228 {
229         int     err;
230         struct file *ofil;
231
232         /*
233          * Search for a free descriptor if needed.
234          */
235         if (fd < 0 || !force) {
236                 if (fd < 0)
237                         fd = 0;
238                 fd = find_free_fildes(fd);
239                 if (fd < 0)
240                         return fd;
241         }
242
243         if ((unsigned )fd >= _sysio_oftab_size) {
244                 err = fd_grow((unsigned )fd + 1);
245                 if (err)
246                         return err;
247         }
248
249         /*
250          * Remember old.
251          */
252         ofil = _sysio_fd_find(fd);
253         /*
254          * Take the entry.
255          */
256         _sysio_oftab[fd] = fil;
257         if (ofil)
258                 F_RELE(ofil);
259
260         return fd;
261 }
262
263 /*
264  * Duplicate old file descriptor.
265  *
266  * If the new file descriptor is less than zero, the new file descriptor
267  * is chosen freely. Otherwise, choose an available descriptor greater
268  * than or equal to the new, if not forced. Otherwise, if forced, (re)use
269  * the new.
270  */
271 int
272 _sysio_fd_dup(int oldfd, int newfd, int force)
273 {
274         struct file *fil;
275         int     fd;
276
277         if (oldfd == newfd && oldfd >= 0)
278                 return newfd;
279
280         fil = _sysio_fd_find(oldfd);
281         if (!fil)
282                 return -EBADF;
283
284         fd = _sysio_fd_set(fil, newfd, force);
285         if (fd >= 0)
286                 F_REF(fil);
287         return fd;
288 }
289
290 int
291 _sysio_fd_close_all()
292 {
293         int     fd;
294         struct file **filp;
295
296         /*
297          * Close all open descriptors.
298          */
299         for (fd = 0, filp = _sysio_oftab;
300              (size_t )fd < _sysio_oftab_size;
301              fd++, filp++) {
302                 if (!*filp)
303                         continue;
304                 F_RELE(*filp);
305                 *filp = NULL;
306         }
307
308         /*
309          * Release current working directory.
310          */
311         if (_sysio_cwd) {
312                 P_RELE(_sysio_cwd);
313                 _sysio_cwd = NULL;
314         }
315
316         return 0;
317 }