Whamcloud - gitweb
- Chain imports on connection, like exports.
[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
70                         if (rc) {
71                                 obdo_free(oa);
72                                 up(&lli->lli_open_sem);
73                                 RETURN(rc);
74                         }
75                         md = lli->lli_smd;
76                 }
77                 if (lli->lli_smd->lmd_object_id == 0)
78                         LBUG();
79                 up(&lli->lli_open_sem);
80         }
81
82         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
83         if (!fd)
84                 GOTO(out, rc = -ENOMEM);
85         memset(fd, 0, sizeof(*fd));
86
87         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
88                       file->f_flags, md, (__u64)(unsigned long)file,
89                       &fd->fd_mdshandle, &req);
90         fd->fd_req = req;
91         ptlrpc_req_finished(req);
92         if (rc)
93                 GOTO(out_req, -abs(rc));
94         if (!fd->fd_mdshandle) {
95                 CERROR("mdc_open didn't assign fd_mdshandle\n");
96                 /* XXX handle this how, abort or is it non-fatal? */
97         }
98
99         if (oa == NULL && (oa = obdo_alloc()) == NULL)
100                 GOTO(out_mdc, rc = -EINVAL);
101
102         oa->o_id = lli->lli_smd->lmd_object_id;
103         oa->o_mode = S_IFREG | inode->i_mode;
104         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID | OBD_MD_FLSIZE;
105         rc = obd_open(ll_i2obdconn(inode), oa, lli->lli_smd);
106         obdo_free(oa);
107         oa = NULL;
108
109         if (rc)
110                 GOTO(out_mdc, rc = -abs(rc));
111
112         file->private_data = fd;
113
114         EXIT;
115
116         return 0;
117 out_mdc:
118         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
119                   S_IFREG, fd->fd_mdshandle, &req);
120 out_req:
121         ptlrpc_free_req(req);
122 //out_fd:
123         obdo_free(oa);
124         kmem_cache_free(ll_file_data_slab, fd);
125         file->private_data = NULL;
126 out:
127         return rc;
128 }
129
130 int ll_size_lock(struct inode *inode, struct lov_stripe_md *md, __u64 start,
131                  int mode, struct lustre_handle **lockhs_p)
132 {
133         struct ll_sb_info *sbi = ll_i2sbi(inode);
134         struct ldlm_extent extent;
135         struct lustre_handle *lockhs = NULL;
136         int rc, flags = 0;
137
138         OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
139         if (lockhs == NULL)
140                 RETURN(-ENOMEM);
141
142         extent.start = start;
143         extent.end = ~0;
144
145         rc = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT, &extent,
146                          sizeof(extent), mode, &flags, ll_lock_callback,
147                          inode, sizeof(*inode), lockhs);
148         if (rc != ELDLM_OK) {
149                 CERROR("lock enqueue: %d\n", rc);
150                 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
151         } else
152                 *lockhs_p = lockhs;
153         RETURN(rc);
154 }
155
156 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *md, int mode,
157                    struct lustre_handle *lockhs)
158 {
159         struct ll_sb_info *sbi = ll_i2sbi(inode);
160         int rc;
161
162         rc = obd_cancel(&sbi->ll_osc_conn, md, mode, lockhs);
163         if (rc != ELDLM_OK) {
164                 CERROR("lock cancel: %d\n", rc);
165                 LBUG();
166         }
167
168         OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
169         RETURN(rc);
170 }
171
172 int ll_file_size(struct inode *inode, struct lov_stripe_md *md)
173 {
174         struct ll_sb_info *sbi = ll_i2sbi(inode);
175         struct lustre_handle *lockhs;
176         struct obdo oa;
177         int err, rc;
178
179         rc = ll_size_lock(inode, md, 0, LCK_PR, &lockhs);
180         if (rc != ELDLM_OK) {
181                 CERROR("lock enqueue: %d\n", rc);
182                 RETURN(rc);
183         }
184
185         oa.o_id = md->lmd_object_id;
186         oa.o_mode = S_IFREG;
187         oa.o_valid = OBD_MD_FLID|OBD_MD_FLMODE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
188         rc = obd_getattr(&sbi->ll_osc_conn, &oa, md);
189         if (!rc)
190                 obdo_to_inode(inode, &oa, oa.o_valid);
191
192         err = ll_size_unlock(inode, md, LCK_PR, lockhs);
193         if (err != ELDLM_OK) {
194                 CERROR("lock cancel: %d\n", err);
195                 LBUG();
196         }
197         RETURN(rc);
198 }
199
200 static int ll_file_release(struct inode *inode, struct file *file)
201 {
202         int rc;
203         struct ptlrpc_request *req = NULL;
204         struct ll_file_data *fd;
205         struct obdo oa;
206         struct ll_sb_info *sbi = ll_i2sbi(inode);
207         struct ll_inode_info *lli = ll_i2info(inode);
208
209         ENTRY;
210
211         fd = (struct ll_file_data *)file->private_data;
212         if (!fd || !fd->fd_mdshandle) {
213                 LBUG();
214                 GOTO(out, rc = -EINVAL);
215         }
216
217         memset(&oa, 0, sizeof(oa));
218         oa.o_id = lli->lli_smd->lmd_object_id;
219         oa.o_mode = S_IFREG;
220         oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
221         rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
222         if (rc)
223                 GOTO(out_fd, abs(rc));
224
225         /* If this fails and we goto out_fd, the file size on the MDS is out of
226          * date.  Is that a big deal? */
227         if (file->f_mode & FMODE_WRITE) {
228                 struct lustre_handle *lockhs;
229
230                 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
231                 if (rc)
232                         GOTO(out_fd, abs(rc));
233
234                 oa.o_id = lli->lli_smd->lmd_object_id;
235                 oa.o_mode = S_IFREG;
236                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
237                         OBD_MD_FLBLOCKS;
238                 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
239                 if (!rc) {
240                         struct iattr attr;
241                         attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
242                                          ATTR_SIZE);
243                         attr.ia_mtime = inode->i_mtime;
244                         attr.ia_ctime = inode->i_ctime;
245                         attr.ia_atime = inode->i_atime;
246                         attr.ia_size = oa.o_size;
247
248                         inode->i_blocks = oa.o_blocks;
249
250                         /* XXX: this introduces a small race that we should
251                          * evaluate */
252                         rc = ll_inode_setattr(inode, &attr, 0);
253                         if (rc) {
254                                 CERROR("failed - %d.\n", rc);
255                                 rc = -EIO; /* XXX - GOTO(out)? -phil */
256                         }
257                 }
258                 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
259                 if (rc) {
260                         CERROR("lock cancel: %d\n", rc);
261                         LBUG();
262                 }
263         }
264
265         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
266                        S_IFREG, fd->fd_mdshandle, &req);
267         ptlrpc_req_finished(req);
268         if (rc) {
269                 if (rc > 0)
270                         rc = -rc;
271                 GOTO(out, rc);
272         }
273         ptlrpc_req_finished(fd->fd_req);
274
275         //ldlm_cli_cancel_unused();
276
277         EXIT;
278
279 out_fd:
280         kmem_cache_free(ll_file_data_slab, fd);
281         file->private_data = NULL;
282 out:
283         return rc;
284 }
285
286
287 static inline void ll_remove_suid(struct inode *inode)
288 {
289         unsigned int mode;
290
291         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
292         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
293
294         /* was any of the uid bits set? */
295         mode &= inode->i_mode;
296         if (mode && !capable(CAP_FSETID)) {
297                 inode->i_mode &= ~mode;
298                 // XXX careful here - we cannot change the size
299         }
300 }
301
302 static void ll_update_atime(struct inode *inode)
303 {
304         struct iattr attr;
305
306         attr.ia_atime = CURRENT_TIME;
307         attr.ia_valid = ATTR_ATIME;
308
309         if (inode->i_atime == attr.ia_atime) return;
310         if (IS_RDONLY(inode)) return;
311         if (IS_NOATIME(inode)) return;
312
313         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
314         ll_inode_setattr(inode, &attr, 0);
315 }
316
317 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
318                      void *data, __u32 data_len)
319 {
320         struct inode *inode = data;
321         struct lustre_handle lockh;
322         int rc;
323         ENTRY;
324
325         if (data_len != sizeof(struct inode))
326                 LBUG();
327
328         if (inode == NULL)
329                 LBUG();
330         down(&inode->i_sem);
331         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
332         /* FIXME: do something better than throwing away everything */
333         invalidate_inode_pages(inode);
334         up(&inode->i_sem);
335
336         ldlm_lock2handle(lock, &lockh);
337         rc = ldlm_cli_cancel(&lockh);
338         if (rc != ELDLM_OK)
339                 CERROR("ldlm_cli_cancel failed: %d\n", rc);
340         RETURN(0);
341 }
342
343 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
344                             loff_t *ppos)
345 {
346         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
347         struct inode *inode = filp->f_dentry->d_inode;
348         struct ll_sb_info *sbi = ll_i2sbi(inode);
349         struct ldlm_extent extent;
350         struct lustre_handle *lockhs = NULL;
351         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
352         int flags = 0;
353         ldlm_error_t err;
354         ssize_t retval;
355         ENTRY;
356
357         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
358                 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
359                 if (!lockhs)
360                         RETURN(-ENOMEM);
361
362                 extent.start = *ppos;
363                 extent.end = *ppos + count;
364                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
365                        inode->i_ino, extent.start, extent.end);
366
367                 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
368                                   &extent, sizeof(extent), LCK_PR, &flags,
369                                   ll_lock_callback, inode, sizeof(*inode),
370                                   lockhs);
371                 if (err != ELDLM_OK) {
372                         OBD_FREE(lockhs, md->lmd_stripe_count *sizeof(*lockhs));
373                         CERROR("lock enqueue: err: %d\n", err);
374                         RETURN(err);
375                 }
376         }
377
378         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
379                inode->i_ino, count, *ppos);
380         retval = generic_file_read(filp, buf, count, ppos);
381
382         if (retval > 0)
383                 ll_update_atime(inode);
384
385         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
386                 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PR, lockhs);
387                 if (err != ELDLM_OK) {
388                         CERROR("lock cancel: err: %d\n", err);
389                         retval = err;
390                 }
391         }
392
393         if (lockhs)
394                 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
395         RETURN(retval);
396 }
397
398 /*
399  * Write to a file (through the page cache).
400  */
401 static ssize_t
402 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
403 {
404         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
405         struct inode *inode = file->f_dentry->d_inode;
406         struct ll_sb_info *sbi = ll_i2sbi(inode);
407         struct ldlm_extent extent;
408         struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
409         struct lov_stripe_md *md = ll_i2info(inode)->lli_smd;
410         int flags = 0;
411         ldlm_error_t err;
412         ssize_t retval;
413         ENTRY;
414
415         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
416                 struct obdo oa;
417                 err = ll_size_lock(inode, md, 0, LCK_PW, &eof_lockhs);
418                 if (err)
419                         RETURN(err);
420
421                 oa.o_id = md->lmd_object_id;
422                 oa.o_mode = inode->i_mode;
423                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
424                         OBD_MD_FLBLOCKS;
425                 retval = obd_getattr(&sbi->ll_osc_conn, &oa, md);
426                 if (retval)
427                         GOTO(out_eof, retval);
428
429                 *ppos = oa.o_size;
430                 obdo_to_inode(inode, &oa, oa.o_valid);
431         }
432
433         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
434                 OBD_ALLOC(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
435                 if (!lockhs)
436                         GOTO(out_eof, retval = -ENOMEM);
437                 extent.start = *ppos;
438                 extent.end = *ppos + count;
439                 CDEBUG(D_INFO, "Locking inode %ld, start %Lu end %Lu\n",
440                        inode->i_ino, extent.start, extent.end);
441
442                 err = obd_enqueue(&sbi->ll_osc_conn, md, NULL, LDLM_EXTENT,
443                                   &extent, sizeof(extent), LCK_PW, &flags,
444                                   ll_lock_callback, inode, sizeof(*inode),
445                                   lockhs);
446                 if (err != ELDLM_OK) {
447                         CERROR("lock enqueue: err: %d\n", err);
448                         GOTO(out_free, retval = err);
449                 }
450         }
451
452         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset %Ld\n",
453                inode->i_ino, (long)count, *ppos);
454
455         retval = generic_file_write(file, buf, count, ppos);
456
457         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK)) {
458                 err = obd_cancel(&sbi->ll_osc_conn, md, LCK_PW, lockhs);
459                 if (err != ELDLM_OK) {
460                         CERROR("lock cancel: err: %d\n", err);
461                         GOTO(out_free, retval = err);
462                 }
463         }
464
465         EXIT;
466  out_free:
467         if (lockhs)
468                 OBD_FREE(lockhs, md->lmd_stripe_count * sizeof(*lockhs));
469
470  out_eof:
471         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
472                 err = ll_size_unlock(inode, md, LCK_PW, eof_lockhs);
473                 if (err && !retval)
474                         retval = err;
475         }
476
477         return retval;
478 }
479
480 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
481                   unsigned long arg)
482 {
483         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
484         int flags;
485
486         switch(cmd) {
487         case LL_IOC_GETFLAGS:
488                 /* Get the current value of the file flags */
489                 return put_user(fd->fd_flags, (int *)arg);
490         case LL_IOC_SETFLAGS:
491         case LL_IOC_CLRFLAGS:
492                 /* Set or clear specific file flags */
493                 /* XXX This probably needs checks to ensure the flags are
494                  *     not abused, and to handle any flag side effects.
495                  */
496                 if (get_user(flags, (int *) arg))
497                         return -EFAULT;
498
499                 if (cmd == LL_IOC_SETFLAGS)
500                         fd->fd_flags |= flags;
501                 else
502                         fd->fd_flags &= ~flags;
503                 return 0;
504
505         /* We need to special case any other ioctls we want to handle,
506          * to send them to the MDS/OST as appropriate and to properly
507          * network encode the arg field.
508         case EXT2_IOC_GETFLAGS:
509         case EXT2_IOC_SETFLAGS:
510         case EXT2_IOC_GETVERSION_OLD:
511         case EXT2_IOC_GETVERSION_NEW:
512         case EXT2_IOC_SETVERSION_OLD:
513         case EXT2_IOC_SETVERSION_NEW:
514         */
515         default:
516                 return -ENOTTY;
517         }
518 }
519
520 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
521 {
522         struct inode *inode = file->f_dentry->d_inode;
523         long long retval;
524         ENTRY;
525
526         switch (origin) {
527         case 2: {
528                 struct ll_inode_info *lli = ll_i2info(inode);
529
530                 retval = ll_file_size(inode, lli->lli_smd);
531                 if (retval)
532                         RETURN(retval);
533
534                 offset += inode->i_size;
535                 break;
536         }
537         case 1:
538                 offset += file->f_pos;
539         }
540         retval = -EINVAL;
541         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
542                 if (offset != file->f_pos) {
543                         file->f_pos = offset;
544                         file->f_reada = 0;
545                         file->f_version = ++event;
546                 }
547                 retval = offset;
548         }
549         RETURN(retval);
550 }
551
552 /* XXX this does not need to do anything for data, it _does_ need to
553    call setattr */
554 int ll_fsync(struct file *file, struct dentry *dentry, int data)
555 {
556         return 0;
557 }
558
559 struct file_operations ll_file_operations = {
560         read:           ll_file_read,
561         write:          ll_file_write,
562         ioctl:          ll_file_ioctl,
563         open:           ll_file_open,
564         release:        ll_file_release,
565         mmap:           generic_file_mmap,
566         llseek:         ll_file_seek,
567         fsync:          NULL
568 };
569
570 struct inode_operations ll_file_inode_operations = {
571         truncate: ll_truncate,
572         setattr: ll_setattr
573 };