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