Whamcloud - gitweb
- Do an additional getattr in ll_lookup2 after we get the lock, to refresh the
[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 lov_stripe_md *md = NULL; 
41         struct ll_sb_info *sbi = ll_i2sbi(inode);
42         struct ll_inode_info *lli = ll_i2info(inode);
43         ENTRY;
44
45         if (file->private_data)
46                 LBUG();
47
48         /*  delayed create of object (intent created inode) */
49         /*  XXX object needs to be cleaned up if mdc_open fails */
50         /*  XXX error handling appropriate here? */
51         if (lli->lli_smd == NULL) {
52                 struct client_obd *mdc = sbi2mdc(ll_s2sbi(inode->i_sb));
53                 struct inode * inode = file->f_dentry->d_inode;
54
55                 down(&lli->lli_open_sem);
56                 /* Check to see if we lost the race */
57                 if (lli->lli_smd == NULL) {
58                         oa = obdo_alloc();
59                         if (!oa) {
60                                 up(&lli->lli_open_sem);
61                                 RETURN(-ENOMEM);
62                         }
63                         oa->o_mode = S_IFREG | 0600;
64                         oa->o_easize = mdc->cl_max_mdsize;
65                         oa->o_id = inode->i_ino;
66                         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
67                                         OBD_MD_FLID;
68                         rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
69                         if (rc) {
70                                 obdo_free(oa);
71                                 up(&lli->lli_open_sem);
72                                 RETURN(rc);
73                         }
74                         md = lli->lli_smd;
75                 }
76                 if (lli->lli_smd->lmd_object_id == 0)
77                         LBUG();
78                 up(&lli->lli_open_sem);
79         }
80
81         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
82         if (!fd)
83                 GOTO(out, rc = -ENOMEM);
84         memset(fd, 0, sizeof(*fd));
85
86         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
87                       file->f_flags, md, (__u64)(unsigned long)file,
88                       &fd->fd_mdshandle, &req);
89         fd->fd_req = req;
90         ptlrpc_req_finished(req);
91         if (rc)
92                 GOTO(out_req, -abs(rc));
93         if (!fd->fd_mdshandle) {
94                 CERROR("mdc_open didn't assign fd_mdshandle\n");
95                 /* XXX handle this how, abort or is it non-fatal? */
96         }
97
98         if (oa == NULL && (oa = obdo_alloc()) == NULL)
99                 GOTO(out_mdc, rc = -EINVAL);
100
101         oa->o_id = lli->lli_smd->lmd_object_id;
102         oa->o_mode = S_IFREG | inode->i_mode;
103         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID;
104         rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
105         obdo_free(oa);
106         oa = NULL;
107
108         if (rc)
109                 GOTO(out_mdc, rc = -abs(rc));
110
111         file->private_data = fd;
112
113         EXIT;
114
115         return 0;
116 out_mdc:
117         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
118                   S_IFREG, fd->fd_mdshandle, &req);
119 out_req:
120         ptlrpc_free_req(req);
121 //out_fd:
122         obdo_free(oa);
123         kmem_cache_free(ll_file_data_slab, fd);
124         file->private_data = NULL;
125 out:
126         return rc;
127 }
128
129 static int ll_file_release(struct inode *inode, struct file *file)
130 {
131         int rc;
132         struct ptlrpc_request *req = NULL;
133         struct ll_file_data *fd;
134         struct obdo *oa;
135         struct ll_sb_info *sbi = ll_i2sbi(inode);
136         struct ll_inode_info *lli = ll_i2info(inode);
137         struct obd_device *obddev = class_conn2obd(&sbi->ll_osc_conn);
138         struct list_head *tmp, *next;
139
140         ENTRY;
141
142         fd = (struct ll_file_data *)file->private_data;
143         if (!fd || !fd->fd_mdshandle) {
144                 LBUG();
145                 GOTO(out, rc = -EINVAL);
146         }
147
148         oa = obdo_alloc();
149         if (oa == NULL) {
150                 LBUG();
151                 GOTO(out_fd, rc = -ENOENT);
152         }
153         oa->o_id = lli->lli_smd->lmd_object_id;
154         oa->o_mode = S_IFREG;
155         oa->o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
156         rc = obd_close(ll_i2obdconn(inode), oa, lli->lli_smd);
157         obdo_free(oa);
158         if (rc)
159                 GOTO(out_fd, abs(rc));
160
161         if (file->f_mode & FMODE_WRITE) {
162                 struct iattr attr;
163                 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
164                                  ATTR_SIZE);
165                 attr.ia_mtime = inode->i_mtime;
166                 attr.ia_ctime = inode->i_ctime;
167                 attr.ia_atime = inode->i_atime;
168                 attr.ia_size = inode->i_size;
169
170                 /* XXX: this introduces a small race that we should evaluate */
171                 rc = ll_inode_setattr(inode, &attr, 0);
172                 if (rc) {
173                         CERROR("failed - %d.\n", rc);
174                         rc = -EIO; /* XXX - GOTO(out)? -phil */
175                 }
176         }
177
178         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
179                        S_IFREG, fd->fd_mdshandle, &req);
180         ptlrpc_req_finished(req);
181         if (rc) {
182                 if (rc > 0)
183                         rc = -rc;
184                 GOTO(out, rc);
185         }
186         ptlrpc_free_req(fd->fd_req);
187
188         l_lock(&obddev->obd_namespace->ns_lock);
189         list_for_each_safe(tmp, next, &lli->lli_osc_locks) {
190                 struct ldlm_lock *lock;
191                 struct lustre_handle lockh;
192                 lock = list_entry(tmp, struct ldlm_lock, l_inode_link);
193
194                 if (!list_empty(&lock->l_inode_link)) {
195                         list_del_init(&lock->l_inode_link);
196                         LDLM_LOCK_PUT(lock);
197                 }
198                 ldlm_lock2handle(lock, &lockh);
199                 rc = ldlm_cli_cancel(&lockh);
200                 if (rc < 0)
201                         CERROR("ldlm_cli_cancel: %d\n", rc);
202         }
203         l_unlock(&obddev->obd_namespace->ns_lock);
204
205         EXIT;
206
207 out_fd:
208         kmem_cache_free(ll_file_data_slab, fd);
209         file->private_data = NULL;
210 out:
211         return rc;
212 }
213
214
215 static inline void ll_remove_suid(struct inode *inode)
216 {
217         unsigned int mode;
218
219         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
220         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
221
222         /* was any of the uid bits set? */
223         mode &= inode->i_mode;
224         if (mode && !capable(CAP_FSETID)) {
225                 inode->i_mode &= ~mode;
226                 // XXX careful here - we cannot change the size
227         }
228 }
229
230 static void ll_update_atime(struct inode *inode)
231 {
232         struct iattr attr;
233
234         attr.ia_atime = CURRENT_TIME;
235         attr.ia_valid = ATTR_ATIME;
236
237         if (inode->i_atime == attr.ia_atime) return;
238         if (IS_RDONLY(inode)) return;
239         if (IS_NOATIME(inode)) return;
240
241         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
242         ll_inode_setattr(inode, &attr, 0);
243 }
244
245 static int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
246                             void *data, __u32 data_len)
247 {
248         struct inode *inode = data;
249         struct lustre_handle lockh;
250         int rc;
251         ENTRY;
252
253         if (data_len != sizeof(struct inode))
254                 LBUG();
255
256         if (inode == NULL)
257                 LBUG();
258         down(&inode->i_sem);
259         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
260         /* FIXME: do something better than throwing away everything */
261         invalidate_inode_pages(inode);
262         up(&inode->i_sem);
263
264         ldlm_lock2handle(lock, &lockh);
265         l_lock(&lock->l_resource->lr_namespace->ns_lock);
266         if (!list_empty(&lock->l_inode_link)) {
267                 list_del_init(&lock->l_inode_link);
268                 LDLM_LOCK_PUT(lock);
269         }
270         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
271
272         rc = ldlm_cli_cancel(&lockh);
273         if (rc != ELDLM_OK)
274                 CERROR("ldlm_cli_cancel failed: %d\n", rc);
275         RETURN(0);
276 }
277
278 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
279                             loff_t *ppos)
280 {
281         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
282         struct inode *inode = filp->f_dentry->d_inode;
283         struct ll_sb_info *sbi = ll_i2sbi(inode);
284         struct ldlm_extent extent;
285         struct lustre_handle *lockhs = NULL;
286         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
287         int flags = 0;
288         ldlm_error_t err;
289         ssize_t retval;
290         ENTRY;
291
292         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
293                 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
294                 if (!lockhs)
295                         RETURN(-ENOMEM);
296
297                 extent.start = *ppos;
298                 extent.end = *ppos + count;
299                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
300                        inode->i_ino, extent.start, extent.end);
301
302                 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
303                                   &extent, sizeof(extent), LCK_PR, &flags,
304                                   ll_lock_callback, inode, sizeof(*inode),
305                                   lockhs);
306                 if (err != ELDLM_OK) {
307                         OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
308                         CERROR("lock enqueue: err: %d\n", err);
309                         RETURN(err);
310                 }
311         }
312
313         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
314                inode->i_ino, count, *ppos);
315         retval = generic_file_read(filp, buf, count, ppos);
316
317         if (retval > 0)
318                 ll_update_atime(inode);
319
320         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
321                 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PR, lockhs);
322                 if (err != ELDLM_OK) {
323                         OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
324                         CERROR("lock cancel: err: %d\n", err);
325                         RETURN(err);
326                 }
327         }
328
329         if (lockhs)
330                 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
331         RETURN(retval);
332 }
333
334 /*
335  * Write to a file (through the page cache).
336  */
337 static ssize_t
338 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
339 {
340         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
341         struct inode *inode = file->f_dentry->d_inode;
342         struct ll_sb_info *sbi = ll_i2sbi(inode);
343         struct ldlm_extent extent;
344         struct lustre_handle *lockhs = NULL;
345         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
346         int flags = 0;
347         ldlm_error_t err;
348         ssize_t retval;
349         ENTRY;
350
351         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
352                 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
353                 if (!lockhs)
354                         RETURN(-ENOMEM); 
355                 /* FIXME: this should check whether O_APPEND is set and adjust
356                  * extent.start accordingly */
357                 extent.start = *ppos;
358                 extent.end = *ppos + count;
359                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
360                        inode->i_ino, extent.start, extent.end);
361
362                 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
363                                   &extent, sizeof(extent), LCK_PW, &flags,
364                                   ll_lock_callback, inode, sizeof(*inode),
365                                   lockhs);
366                 if (err != ELDLM_OK) {
367                         OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
368                         CERROR("lock enqueue: err: %d\n", err);
369                         RETURN(err);
370                 }
371         }
372
373         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
374                inode->i_ino, (long)count, *ppos);
375
376         retval = generic_file_write(file, buf, count, ppos);
377
378         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
379                 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PW, lockhs);
380                 if (err != ELDLM_OK) {
381                         OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
382                         CERROR("lock cancel: err: %d\n", err);
383                         RETURN(err);
384                 }
385         }
386
387         if (lockhs)
388                 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
389         RETURN(retval);
390 }
391
392 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
393                   unsigned long arg)
394 {
395         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
396         int flags;
397
398         switch(cmd) {
399         case LL_IOC_GETFLAGS:
400                 /* Get the current value of the file flags */
401                 return put_user(fd->fd_flags, (int *)arg);
402         case LL_IOC_SETFLAGS:
403         case LL_IOC_CLRFLAGS:
404                 /* Set or clear specific file flags */
405                 /* XXX This probably needs checks to ensure the flags are
406                  *     not abused, and to handle any flag side effects.
407                  */
408                 if (get_user(flags, (int *) arg))
409                         return -EFAULT;
410
411                 if (cmd == LL_IOC_SETFLAGS)
412                         fd->fd_flags |= flags;
413                 else
414                         fd->fd_flags &= ~flags;
415                 return 0;
416
417         /* We need to special case any other ioctls we want to handle,
418          * to send them to the MDS/OST as appropriate and to properly
419          * network encode the arg field.
420         case EXT2_IOC_GETFLAGS:
421         case EXT2_IOC_SETFLAGS:
422         case EXT2_IOC_GETVERSION_OLD:
423         case EXT2_IOC_GETVERSION_NEW:
424         case EXT2_IOC_SETVERSION_OLD:
425         case EXT2_IOC_SETVERSION_NEW:
426         */
427         default:
428                 return -ENOTTY;
429         }
430 }
431
432 /* XXX this does not need to do anything for data, it _does_ need to
433    call setattr */
434 int ll_fsync(struct file *file, struct dentry *dentry, int data)
435 {
436         return 0;
437 }
438
439 struct file_operations ll_file_operations = {
440         read:           ll_file_read,
441         write:          ll_file_write,
442         ioctl:          ll_file_ioctl,
443         open:           ll_file_open,
444         release:        ll_file_release,
445         mmap:           generic_file_mmap,
446         fsync:          NULL
447 };
448
449 struct inode_operations ll_file_inode_operations = {
450         truncate: ll_truncate,
451         setattr: ll_setattr
452 };