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