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