Whamcloud - gitweb
aa1633303a9a59eb9903cef78a81acf1a96fc5d3
[fs/lustre-release.git] / lustre / llite / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/ext2/file.c
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copyright (C) 1992, 1993, 1994, 1995
10  * Remy Card (card@masi.ibp.fr)
11  * Laboratoire MASI - Institut Blaise Pascal
12  * Universite Pierre et Marie Curie (Paris VI)
13  *
14  *  from
15  *
16  *  linux/fs/minix/file.c
17  *
18  *  Copyright (C) 1991, 1992  Linus Torvalds
19  *
20  *  ext2 fs regular file handling primitives
21  *
22  *  64-bit file support on 64-bit platforms by Jakub Jelinek
23  *      (jj@sunsite.ms.mff.cuni.cz)
24  */
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <linux/lustre_dlm.h>
29 #include <linux/lustre_lite.h>
30
31 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
32 extern int ll_setattr(struct dentry *de, struct iattr *attr);
33
34 static int ll_file_open(struct inode *inode, struct file *file)
35 {
36         int rc;
37         struct ptlrpc_request *req = NULL;
38         struct ll_file_data *fd;
39         struct obdo *oa = NULL;
40         struct ll_sb_info *sbi = ll_i2sbi(inode);
41         struct ll_inode_info *lli = ll_i2info(inode);
42         ENTRY;
43
44         if (file->private_data)
45                 LBUG();
46
47         /*  delayed create of object (intent created inode) */
48         /*  XXX object needs to be cleaned up if mdc_open fails */
49         /*  XXX error handling appropriate here? */
50         if (lli->lli_obdo == NULL) {
51                 struct inode * inode = file->f_dentry->d_inode;
52
53                 oa = lli->lli_obdo = obdo_alloc();
54                 oa->o_valid = OBD_MD_FLMODE;
55                 oa->o_mode = S_IFREG | 0600;
56                 rc = obd_create(ll_i2obdconn(inode), oa);
57                 if (rc)
58                         RETURN(rc);
59                 lli->lli_flags &= ~OBD_FL_CREATEONOPEN;
60         }
61
62         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
63         if (!fd)
64                 GOTO(out, rc = -ENOMEM);
65         memset(fd, 0, sizeof(*fd));
66
67         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG, file->f_flags,
68                       oa, (__u64)(unsigned long)file, &fd->fd_mdshandle, &req);
69         fd->fd_req = req;
70         ptlrpc_req_finished(req);
71         if (rc)
72                 GOTO(out_req, -abs(rc));
73         if (!fd->fd_mdshandle) {
74                 CERROR("mdc_open didn't assign fd_mdshandle\n");
75                 /* XXX handle this how, abort or is it non-fatal? */
76         }
77         if (!fd->fd_mdshandle)
78                 CERROR("mdc_open didn't assign fd_mdshandle\n");
79
80         oa = lli->lli_obdo;
81         if (oa == NULL) {
82                 LBUG();
83                 GOTO(out_mdc, rc = -EINVAL);
84         }
85
86         rc = obd_open(ll_i2obdconn(inode), oa);
87         if (rc)
88                 GOTO(out_mdc, rc = -abs(rc));
89
90         file->private_data = fd;
91
92         EXIT;
93
94         return 0;
95 out_mdc:
96         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
97                   S_IFREG, fd->fd_mdshandle, &req);
98 out_req:
99         ptlrpc_free_req(req);
100 //out_fd:
101         kmem_cache_free(ll_file_data_slab, fd);
102         file->private_data = NULL;
103 out:
104         return rc;
105 }
106
107 static int ll_file_release(struct inode *inode, struct file *file)
108 {
109         int rc;
110         struct ptlrpc_request *req = NULL;
111         struct ll_file_data *fd;
112         struct obdo *oa;
113         struct ll_sb_info *sbi = ll_i2sbi(inode);
114
115         ENTRY;
116
117         fd = (struct ll_file_data *)file->private_data;
118         if (!fd || !fd->fd_mdshandle) {
119                 LBUG();
120                 GOTO(out, rc = -EINVAL);
121         }
122
123         oa = ll_i2info(inode)->lli_obdo;
124         if (oa == NULL) {
125                 LBUG();
126                 GOTO(out_fd, rc = -ENOENT);
127         }
128         rc = obd_close(ll_i2obdconn(inode), oa);
129         if (rc)
130                 GOTO(out_fd, abs(rc));
131
132         if (file->f_mode & FMODE_WRITE) {
133                 struct iattr attr;
134                 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
135                 attr.ia_mtime = inode->i_mtime;
136                 attr.ia_ctime = inode->i_ctime;
137                 attr.ia_atime = inode->i_atime;
138                 attr.ia_size = inode->i_size;
139
140                 /* XXX: this introduces a small race that we should evaluate */
141                 rc = ll_inode_setattr(inode, &attr, 0);
142                 if (rc) {
143                         CERROR("failed - %d.\n", rc);
144                         rc = -EIO; /* XXX - GOTO(out)? -phil */
145                 }
146         }
147
148         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
149                        S_IFREG, fd->fd_mdshandle, &req);
150         ptlrpc_req_finished(req);
151         if (rc) {
152                 if (rc > 0)
153                         rc = -rc;
154                 GOTO(out, rc);
155         }
156         ptlrpc_free_req(fd->fd_req);
157
158         EXIT;
159
160 out_fd:
161         kmem_cache_free(ll_file_data_slab, fd);
162         file->private_data = NULL;
163 out:
164         return rc;
165 }
166
167
168 static inline void ll_remove_suid(struct inode *inode)
169 {
170         unsigned int mode;
171
172         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
173         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
174
175         /* was any of the uid bits set? */
176         mode &= inode->i_mode;
177         if (mode && !capable(CAP_FSETID)) {
178                 inode->i_mode &= ~mode;
179                 // XXX careful here - we cannot change the size
180         }
181 }
182
183 static void ll_update_atime(struct inode *inode)
184 {
185         struct iattr attr;
186
187         attr.ia_atime = CURRENT_TIME;
188         attr.ia_valid = ATTR_ATIME;
189
190         if (inode->i_atime == attr.ia_atime) return;
191         if (IS_RDONLY(inode)) return;
192         if (IS_NOATIME(inode)) return;
193
194         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
195         ll_inode_setattr(inode, &attr, 0);
196 }
197
198 static int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
199                             void *data, __u32 data_len)
200 {
201         struct inode *inode = lock->l_data;
202         ENTRY;
203
204         if (new == NULL) {
205                 /* Completion AST.  Do nothing. */
206                 RETURN(0);
207         }
208
209         if (data_len != sizeof(struct inode))
210                 LBUG();
211
212         /* FIXME: do something better than throwing away everything */
213         if (inode == NULL)
214                 LBUG();
215         down(&inode->i_sem);
216         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
217         invalidate_inode_pages(inode);
218         up(&inode->i_sem);
219
220         if (ldlm_cli_cancel(lock->l_client, lock) < 0)
221                 LBUG();
222         RETURN(0);
223 }
224
225 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
226                             loff_t *ppos)
227 {
228         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
229         struct inode *inode = filp->f_dentry->d_inode;
230         struct ll_sb_info *sbi = ll_i2sbi(inode);
231         struct ldlm_extent extent;
232         struct lustre_handle lockh;
233         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
234         int flags = 0;
235         ldlm_error_t err;
236         ssize_t retval;
237         ENTRY;
238
239         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
240                 extent.start = *ppos;
241                 extent.end = *ppos + count;
242                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
243                        inode->i_ino, extent.start, extent.end);
244
245                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
246                                   &extent, sizeof(extent), LCK_PR, &flags,
247                                   ll_lock_callback, inode, sizeof(*inode),
248                                   &lockh);
249                 if (err != ELDLM_OK)
250                         CERROR("lock enqueue: err: %d\n", err);
251                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
252         }
253
254         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
255                inode->i_ino, count, *ppos);
256         retval = generic_file_read(filp, buf, count, ppos);
257
258         if (retval > 0)
259                 ll_update_atime(inode);
260
261         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
262                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PR, &lockh);
263                 if (err != ELDLM_OK)
264                         CERROR("lock cancel: err: %d\n", err);
265         }
266
267         RETURN(retval);
268 }
269
270 /*
271  * Write to a file (through the page cache).
272  */
273 static ssize_t
274 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
275 {
276         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
277         struct inode *inode = file->f_dentry->d_inode;
278         struct ll_sb_info *sbi = ll_i2sbi(inode);
279         struct ldlm_extent extent;
280         struct lustre_handle lockh;
281         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
282         int flags = 0;
283         ldlm_error_t err;
284         ssize_t retval;
285         ENTRY;
286
287         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
288                 /* FIXME: this should check whether O_APPEND is set and adjust
289                  * extent.start accordingly */
290                 extent.start = *ppos;
291                 extent.end = *ppos + count;
292                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
293                        inode->i_ino, extent.start, extent.end);
294
295                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
296                                   &extent, sizeof(extent), LCK_PW, &flags,
297                                   ll_lock_callback, inode, sizeof(*inode),
298                                   &lockh);
299                 if (err != ELDLM_OK)
300                         CERROR("lock enqueue: err: %d\n", err);
301                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
302         }
303
304         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
305                inode->i_ino, (long)count, *ppos);
306
307         retval = generic_file_write(file, buf, count, ppos);
308
309         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
310                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PW, &lockh);
311                 if (err != ELDLM_OK)
312                         CERROR("lock cancel: err: %d\n", err);
313         }
314
315         RETURN(retval);
316 }
317
318 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
319                   unsigned long arg)
320 {
321         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
322         int flags;
323
324         switch(cmd) {
325         case LL_IOC_GETFLAGS:
326                 /* Get the current value of the file flags */
327                 return put_user(fd->fd_flags, (int *)arg);
328         case LL_IOC_SETFLAGS:
329         case LL_IOC_CLRFLAGS:
330                 /* Set or clear specific file flags */
331                 /* XXX This probably needs checks to ensure the flags are
332                  *     not abused, and to handle any flag side effects.
333                  */
334                 if (get_user(flags, (int *) arg))
335                         return -EFAULT;
336
337                 if (cmd == LL_IOC_SETFLAGS)
338                         fd->fd_flags |= flags;
339                 else
340                         fd->fd_flags &= ~flags;
341                 return 0;
342
343         /* We need to special case any other ioctls we want to handle,
344          * to send them to the MDS/OST as appropriate and to properly
345          * network encode the arg field.
346         case EXT2_IOC_GETFLAGS:
347         case EXT2_IOC_SETFLAGS:
348         case EXT2_IOC_GETVERSION_OLD:
349         case EXT2_IOC_GETVERSION_NEW:
350         case EXT2_IOC_SETVERSION_OLD:
351         case EXT2_IOC_SETVERSION_NEW:
352         */
353         default:
354                 return -ENOTTY;
355         }
356 }
357
358 /* XXX this does not need to do anything for data, it _does_ need to
359    call setattr */
360 int ll_fsync(struct file *file, struct dentry *dentry, int data)
361 {
362         return 0;
363 }
364
365 struct file_operations ll_file_operations = {
366         read:           ll_file_read,
367         write:          ll_file_write,
368         ioctl:          ll_file_ioctl,
369         open:           ll_file_open,
370         release:        ll_file_release,
371         mmap:           generic_file_mmap,
372         fsync:          NULL
373 };
374
375 struct inode_operations ll_file_inode_operations = {
376         truncate: ll_truncate,
377         setattr: ll_setattr
378 };