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