Whamcloud - gitweb
1fe90c5132439c684cd22485b6c9bdfc7410711d
[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 | inode->i_mode,
68                       file->f_flags,
69                       oa, (__u64)(unsigned long)file, &fd->fd_mdshandle, &req);
70         fd->fd_req = req;
71         ptlrpc_req_finished(req);
72         if (rc)
73                 GOTO(out_req, -abs(rc));
74         if (!fd->fd_mdshandle) {
75                 CERROR("mdc_open didn't assign fd_mdshandle\n");
76                 /* XXX handle this how, abort or is it non-fatal? */
77         }
78         if (!fd->fd_mdshandle)
79                 CERROR("mdc_open didn't assign fd_mdshandle\n");
80
81         oa = lli->lli_obdo;
82         if (oa == NULL) {
83                 LBUG();
84                 GOTO(out_mdc, rc = -EINVAL);
85         }
86
87         rc = obd_open(ll_i2obdconn(inode), oa);
88         if (rc)
89                 GOTO(out_mdc, rc = -abs(rc));
90
91         file->private_data = fd;
92
93         EXIT;
94
95         return 0;
96 out_mdc:
97         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
98                   S_IFREG, fd->fd_mdshandle, &req);
99 out_req:
100         ptlrpc_free_req(req);
101 //out_fd:
102         kmem_cache_free(ll_file_data_slab, fd);
103         file->private_data = NULL;
104 out:
105         return rc;
106 }
107
108 static int ll_file_release(struct inode *inode, struct file *file)
109 {
110         int rc;
111         struct ptlrpc_request *req = NULL;
112         struct ll_file_data *fd;
113         struct obdo *oa;
114         struct ll_sb_info *sbi = ll_i2sbi(inode);
115
116         ENTRY;
117
118         fd = (struct ll_file_data *)file->private_data;
119         if (!fd || !fd->fd_mdshandle) {
120                 LBUG();
121                 GOTO(out, rc = -EINVAL);
122         }
123
124         oa = ll_i2info(inode)->lli_obdo;
125         if (oa == NULL) {
126                 LBUG();
127                 GOTO(out_fd, rc = -ENOENT);
128         }
129         rc = obd_close(ll_i2obdconn(inode), oa);
130         if (rc)
131                 GOTO(out_fd, abs(rc));
132
133         if (file->f_mode & FMODE_WRITE) {
134                 struct iattr attr;
135                 attr.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_ATIME | ATTR_SIZE;
136                 attr.ia_mtime = inode->i_mtime;
137                 attr.ia_ctime = inode->i_ctime;
138                 attr.ia_atime = inode->i_atime;
139                 attr.ia_size = inode->i_size;
140
141                 /* XXX: this introduces a small race that we should evaluate */
142                 rc = ll_inode_setattr(inode, &attr, 0);
143                 if (rc) {
144                         CERROR("failed - %d.\n", rc);
145                         rc = -EIO; /* XXX - GOTO(out)? -phil */
146                 }
147         }
148
149         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
150                        S_IFREG, fd->fd_mdshandle, &req);
151         ptlrpc_req_finished(req);
152         if (rc) {
153                 if (rc > 0)
154                         rc = -rc;
155                 GOTO(out, rc);
156         }
157         ptlrpc_free_req(fd->fd_req);
158
159         EXIT;
160
161 out_fd:
162         kmem_cache_free(ll_file_data_slab, fd);
163         file->private_data = NULL;
164 out:
165         return rc;
166 }
167
168
169 static inline void ll_remove_suid(struct inode *inode)
170 {
171         unsigned int mode;
172
173         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
174         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
175
176         /* was any of the uid bits set? */
177         mode &= inode->i_mode;
178         if (mode && !capable(CAP_FSETID)) {
179                 inode->i_mode &= ~mode;
180                 // XXX careful here - we cannot change the size
181         }
182 }
183
184 static void ll_update_atime(struct inode *inode)
185 {
186         struct iattr attr;
187
188         attr.ia_atime = CURRENT_TIME;
189         attr.ia_valid = ATTR_ATIME;
190
191         if (inode->i_atime == attr.ia_atime) return;
192         if (IS_RDONLY(inode)) return;
193         if (IS_NOATIME(inode)) return;
194
195         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
196         ll_inode_setattr(inode, &attr, 0);
197 }
198
199 static int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
200                             void *data, __u32 data_len,
201                             struct ptlrpc_request **reqp)
202 {
203         struct inode *inode = lock->l_data;
204         struct lustre_handle lockh;
205         ENTRY;
206
207         if (new == NULL) {
208                 /* Completion AST.  Do nothing. */
209                 RETURN(0);
210         }
211
212         if (data_len != sizeof(struct inode))
213                 LBUG();
214
215         /* FIXME: do something better than throwing away everything */
216         if (inode == NULL)
217                 LBUG();
218         down(&inode->i_sem);
219         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
220         invalidate_inode_pages(inode);
221         up(&inode->i_sem);
222
223         ldlm_lock2handle(lock, &lockh);
224         if (ldlm_cli_cancel(lock->l_client, &lockh) < 0)
225                 LBUG();
226         RETURN(0);
227 }
228
229 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
230                             loff_t *ppos)
231 {
232         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
233         struct inode *inode = filp->f_dentry->d_inode;
234         struct ll_sb_info *sbi = ll_i2sbi(inode);
235         struct ldlm_extent extent;
236         struct lustre_handle lockh;
237         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
238         int flags = 0;
239         ldlm_error_t err;
240         ssize_t retval;
241         ENTRY;
242
243         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
244                 extent.start = *ppos;
245                 extent.end = *ppos + count;
246                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
247                        inode->i_ino, extent.start, extent.end);
248
249                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
250                                   &extent, sizeof(extent), LCK_PR, &flags,
251                                   ll_lock_callback, inode, sizeof(*inode),
252                                   &lockh);
253                 if (err != ELDLM_OK)
254                         CERROR("lock enqueue: err: %d\n", err);
255                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
256         }
257
258         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
259                inode->i_ino, count, *ppos);
260         retval = generic_file_read(filp, buf, count, ppos);
261
262         if (retval > 0)
263                 ll_update_atime(inode);
264
265         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
266                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PR, &lockh);
267                 if (err != ELDLM_OK)
268                         CERROR("lock cancel: err: %d\n", err);
269         }
270
271         RETURN(retval);
272 }
273
274 /*
275  * Write to a file (through the page cache).
276  */
277 static ssize_t
278 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
279 {
280         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
281         struct inode *inode = file->f_dentry->d_inode;
282         struct ll_sb_info *sbi = ll_i2sbi(inode);
283         struct ldlm_extent extent;
284         struct lustre_handle lockh;
285         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino};
286         int flags = 0;
287         ldlm_error_t err;
288         ssize_t retval;
289         ENTRY;
290
291         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
292                 /* FIXME: this should check whether O_APPEND is set and adjust
293                  * extent.start accordingly */
294                 extent.start = *ppos;
295                 extent.end = *ppos + count;
296                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
297                        inode->i_ino, extent.start, extent.end);
298
299                 err = obd_enqueue(&sbi->ll_osc_conn, NULL, res_id, LDLM_EXTENT,
300                                   &extent, sizeof(extent), LCK_PW, &flags,
301                                   ll_lock_callback, inode, sizeof(*inode),
302                                   &lockh);
303                 if (err != ELDLM_OK)
304                         CERROR("lock enqueue: err: %d\n", err);
305                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
306         }
307
308         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
309                inode->i_ino, (long)count, *ppos);
310
311         retval = generic_file_write(file, buf, count, ppos);
312
313         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
314                 err = obd_cancel(&sbi->ll_osc_conn, LCK_PW, &lockh);
315                 if (err != ELDLM_OK)
316                         CERROR("lock cancel: err: %d\n", err);
317         }
318
319         RETURN(retval);
320 }
321
322 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
323                   unsigned long arg)
324 {
325         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
326         int flags;
327
328         switch(cmd) {
329         case LL_IOC_GETFLAGS:
330                 /* Get the current value of the file flags */
331                 return put_user(fd->fd_flags, (int *)arg);
332         case LL_IOC_SETFLAGS:
333         case LL_IOC_CLRFLAGS:
334                 /* Set or clear specific file flags */
335                 /* XXX This probably needs checks to ensure the flags are
336                  *     not abused, and to handle any flag side effects.
337                  */
338                 if (get_user(flags, (int *) arg))
339                         return -EFAULT;
340
341                 if (cmd == LL_IOC_SETFLAGS)
342                         fd->fd_flags |= flags;
343                 else
344                         fd->fd_flags &= ~flags;
345                 return 0;
346
347         /* We need to special case any other ioctls we want to handle,
348          * to send them to the MDS/OST as appropriate and to properly
349          * network encode the arg field.
350         case EXT2_IOC_GETFLAGS:
351         case EXT2_IOC_SETFLAGS:
352         case EXT2_IOC_GETVERSION_OLD:
353         case EXT2_IOC_GETVERSION_NEW:
354         case EXT2_IOC_SETVERSION_OLD:
355         case EXT2_IOC_SETVERSION_NEW:
356         */
357         default:
358                 return -ENOTTY;
359         }
360 }
361
362 /* XXX this does not need to do anything for data, it _does_ need to
363    call setattr */
364 int ll_fsync(struct file *file, struct dentry *dentry, int data)
365 {
366         return 0;
367 }
368
369 struct file_operations ll_file_operations = {
370         read:           ll_file_read,
371         write:          ll_file_write,
372         ioctl:          ll_file_ioctl,
373         open:           ll_file_open,
374         release:        ll_file_release,
375         mmap:           generic_file_mmap,
376         fsync:          NULL
377 };
378
379 struct inode_operations ll_file_inode_operations = {
380         truncate: ll_truncate,
381         setattr: ll_setattr
382 };