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