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