Whamcloud - gitweb
LU-4725 obd: lu_object_find_at hung
[fs/lustre-release.git] / libsysio / src / truncate.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 <unistd.h>
45 #include <string.h>
46 #include <fcntl.h>
47 #include <errno.h>
48 #include <assert.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/queue.h>
52
53 #include "sysio.h"
54 #include "inode.h"
55 #include "file.h"
56 #include "fs.h"
57 #include "mount.h"
58
59 #include "sysio-symbols.h"
60
61 /*
62  * Truncate file, given path (alias) or index node.
63  */
64 static int
65 do_truncate(struct pnode *pno, struct inode *ino, _SYSIO_OFF_T length)
66 {
67         struct intnl_stat stbuf;
68         unsigned mask;
69
70         if (length < 0)
71                 return -EINVAL;
72
73         if (!ino && pno->p_base->pb_ino)
74                 ino = pno->p_base->pb_ino;
75         if (!ino)
76                 return -EBADF;
77         if (S_ISDIR(ino->i_stbuf.st_mode))              /* for others too? */
78                 return -EISDIR;
79         if (!S_ISREG(ino->i_stbuf.st_mode))
80                 return -EINVAL;
81
82         (void )memset(&stbuf, 0, sizeof(stbuf));
83         stbuf.st_size = length;
84         mask = SETATTR_LEN;
85         return _sysio_setattr(pno, ino, mask, &stbuf);
86 }
87
88 static int
89 PREPEND(_, SYSIO_INTERFACE_NAME(truncate))(const char *path, 
90                                            _SYSIO_OFF_T length)
91 {
92         int     err;
93         struct pnode *pno;
94         SYSIO_INTERFACE_DISPLAY_BLOCK;
95
96         SYSIO_INTERFACE_ENTER;
97         err = _sysio_namei(_sysio_cwd, path, 0, NULL, &pno);
98         if (err)
99                 goto out;
100         err = do_truncate(pno, pno->p_base->pb_ino, length);
101         P_RELE(pno);
102
103 out:
104         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
105 }
106
107 #ifdef _LARGEFILE64_SOURCE
108 #undef truncate64
109 sysio_sym_weak_alias(PREPEND(_, SYSIO_INTERFACE_NAME(truncate)),
110                      SYSIO_INTERFACE_NAME(truncate64))
111
112 #undef truncate
113 int
114 SYSIO_INTERFACE_NAME(truncate)(const char *path, off_t length)
115 {
116
117         return PREPEND(_, SYSIO_INTERFACE_NAME(truncate))(path, length);
118 }
119 #else
120 #undef truncate
121 sysio_sym_weak_alias(PREPEND(_, SYSIO_INTERFACE_NAME(truncate)),
122                      SYSIO_INTERFACE_NAME(truncate))
123 #endif
124
125 static int
126 PREPEND(_, SYSIO_INTERFACE_NAME(ftruncate))(int fd, _SYSIO_OFF_T length)
127 {
128         int     err;
129         struct file *fil;
130         SYSIO_INTERFACE_DISPLAY_BLOCK;
131
132         SYSIO_INTERFACE_ENTER;
133         err = 0;
134         fil = _sysio_fd_find(fd);
135         if (!fil) {
136                 err = -EBADF;
137                 goto out;
138         }
139         if (!F_CHKRW(fil, 'w')) {
140                 err = -EBADF;
141                 goto out;
142         }
143         err = do_truncate(NULL, fil->f_ino, length);
144 out:
145         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
146 }
147
148 #ifdef _LARGEFILE64_SOURCE
149 #undef ftruncate64
150 sysio_sym_weak_alias(PREPEND(_, SYSIO_INTERFACE_NAME(ftruncate)), 
151                      SYSIO_INTERFACE_NAME(ftruncate64))
152
153 #undef ftruncate
154 int
155 SYSIO_INTERFACE_NAME(ftruncate)(int fd, off_t length)
156 {
157
158         return PREPEND(_, SYSIO_INTERFACE_NAME(ftruncate))(fd, length);
159 }
160 #else
161 #undef ftruncate
162 sysio_sym_weak_alias(PREPEND(_, SYSIO_INTERFACE_NAME(ftruncate)), 
163                      SYSIO_INTERFACE_NAME(ftruncate))
164 #endif