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