Whamcloud - gitweb
b=255
[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         atomic_inc(&lli->lli_open_count);
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 0
278 #error "This should only be done on the node that already has the EOF lock"
279 #error "and only in the case where the file size actually changed.  For now"
280 #error "we don't care about the size on the MDS, since we never use it (the"
281 #error "OST always has the authoritative size and we don't even use the MDS."
282         /* If this fails and we goto out_fd, the file size on the MDS is out of
283          * date.  Is that a big deal? */
284         if (file->f_mode & FMODE_WRITE) {
285                 struct lustre_handle *lockhs;
286
287                 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
288                 if (rc)
289                         GOTO(out_mdc, -abs(rc));
290
291                 oa.o_id = lsm->lsm_object_id;
292                 oa.o_mode = S_IFREG;
293                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
294                         OBD_MD_FLBLOCKS;
295                 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
296                 if (!rc) {
297                         struct iattr attr;
298                         attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
299                                          ATTR_SIZE);
300                         attr.ia_mtime = inode->i_mtime;
301                         attr.ia_ctime = inode->i_ctime;
302                         attr.ia_atime = inode->i_atime;
303                         attr.ia_size = oa.o_size;
304
305                         inode->i_blocks = oa.o_blocks;
306
307                         /* XXX: this introduces a small race that we should
308                          * evaluate */
309                         rc = ll_inode_setattr(inode, &attr, 0);
310                 }
311                 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
312                 if (rc2) {
313                         CERROR("lock cancel: %d\n", rc);
314                         LBUG();
315                         if (!rc)
316                                 rc = rc2;
317                 }
318         }
319 #endif
320
321 out_mdc:
322         rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
323                         S_IFREG, &fd->fd_mdshandle, &req);
324         ptlrpc_req_finished(req);
325         if (rc2) {
326                 if (!rc)
327                         rc = -abs(rc2);
328                 GOTO(out_fd, rc);
329         }
330         CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op "
331                "%d->%s:%d\n", fd->fd_req, fd->fd_req->rq_xid,
332                fd->fd_req->rq_repmsg->transno, fd->fd_req->rq_reqmsg->opc,
333                fd->fd_req->rq_import->imp_connection->c_remote_uuid,
334                fd->fd_req->rq_import->imp_client->cli_request_portal);
335         ptlrpc_req_finished(fd->fd_req);
336
337         if (atomic_dec_and_test(&lli->lli_open_count)) {
338                 CDEBUG(D_INFO, "last close, cancelling unused locks\n");
339                 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
340                 if (rc)
341                         CERROR("obd_cancel_unused: %d\n", rc);
342         } else {
343                 CDEBUG(D_INFO, "not last close, not cancelling unused locks\n");
344         }
345
346         EXIT;
347
348 out_fd:
349         fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
350         file->private_data = NULL;
351         kmem_cache_free(ll_file_data_slab, fd);
352 out:
353         return rc;
354 }
355
356 static inline void ll_remove_suid(struct inode *inode)
357 {
358         unsigned int mode;
359
360         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
361         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
362
363         /* was any of the uid bits set? */
364         mode &= inode->i_mode;
365         if (mode && !capable(CAP_FSETID)) {
366                 inode->i_mode &= ~mode;
367                 // XXX careful here - we cannot change the size
368         }
369 }
370
371 static void ll_update_atime(struct inode *inode)
372 {
373         struct iattr attr;
374
375         attr.ia_atime = CURRENT_TIME;
376         attr.ia_valid = ATTR_ATIME;
377
378         if (inode->i_atime == attr.ia_atime) return;
379         if (IS_RDONLY(inode)) return;
380         if (IS_NOATIME(inode)) return;
381
382         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
383         ll_inode_setattr(inode, &attr, 0);
384 }
385
386 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
387                      void *data, __u32 data_len, int flag)
388 {
389         struct inode *inode = data;
390         struct lustre_handle lockh;
391         int rc;
392         ENTRY;
393
394         if (data_len != sizeof(struct inode))
395                 LBUG();
396
397         if (inode == NULL)
398                 LBUG();
399
400         switch (flag) {
401         case LDLM_CB_BLOCKING:
402                 ldlm_lock2handle(lock, &lockh);
403                 rc = ldlm_cli_cancel(&lockh);
404                 if (rc != ELDLM_OK)
405                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
406                 break;
407         case LDLM_CB_CANCELING:
408                 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
409                 /* FIXME: do something better than throwing away everything */
410                 //down(&inode->i_sem);
411                 ll_invalidate_inode_pages(inode);
412                 //up(&inode->i_sem);
413                 break;
414         default:
415                 LBUG();
416         }
417
418         RETURN(0);
419 }
420
421 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
422                             loff_t *ppos)
423 {
424         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
425         struct inode *inode = filp->f_dentry->d_inode;
426         struct ll_sb_info *sbi = ll_i2sbi(inode);
427         struct lustre_handle *lockhs = NULL;
428         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
429         int flags = 0;
430         ldlm_error_t err;
431         ssize_t retval;
432         ENTRY;
433
434         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
435             !(sbi->ll_flags & LL_SBI_NOLCK)) {
436                 struct ldlm_extent extent;
437                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
438                 if (!lockhs)
439                         RETURN(-ENOMEM);
440
441                 extent.start = *ppos;
442                 extent.end = *ppos + count;
443                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
444                        inode->i_ino, extent.start, extent.end);
445
446                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
447                                   &extent, sizeof(extent), LCK_PR, &flags,
448                                   ll_lock_callback, inode, sizeof(*inode),
449                                   lockhs);
450                 if (err != ELDLM_OK) {
451                         OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
452                         CERROR("lock enqueue: err: %d\n", err);
453                         RETURN(err);
454                 }
455         }
456
457         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
458                inode->i_ino, count, *ppos);
459         retval = generic_file_read(filp, buf, count, ppos);
460
461         if (retval > 0)
462                 ll_update_atime(inode);
463
464         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
465             !(sbi->ll_flags & LL_SBI_NOLCK)) {
466                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
467                 if (err != ELDLM_OK) {
468                         CERROR("lock cancel: err: %d\n", err);
469                         retval = err;
470                 }
471         }
472
473         if (lockhs)
474                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
475         RETURN(retval);
476 }
477
478 /*
479  * Write to a file (through the page cache).
480  */
481 static ssize_t
482 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
483 {
484         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
485         struct inode *inode = file->f_dentry->d_inode;
486         struct ll_sb_info *sbi = ll_i2sbi(inode);
487         struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
488         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
489         int flags = 0;
490         ldlm_error_t err;
491         ssize_t retval;
492         ENTRY;
493
494         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
495                 struct obdo *oa;
496
497                 oa = obdo_alloc();
498                 if (!oa)
499                         RETURN(-ENOMEM);
500
501                 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
502                 if (err) {
503                         obdo_free(oa);
504                         RETURN(err);
505                 }
506
507                 oa->o_id = lsm->lsm_object_id;
508                 oa->o_mode = inode->i_mode;
509                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
510                         OBD_MD_FLBLOCKS;
511                 obd_handle2oa(oa, &fd->fd_osthandle);
512                 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
513                 if (retval) {
514                         obdo_free(oa);
515                         GOTO(out_eof, retval);
516                 }
517
518                 *ppos = oa->o_size;
519                 obdo_to_inode(inode, oa, oa->o_valid);
520                 obdo_free(oa);
521         }
522
523         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
524             !(sbi->ll_flags & LL_SBI_NOLCK)) {
525                 struct ldlm_extent extent;
526                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
527                 if (!lockhs)
528                         GOTO(out_eof, retval = -ENOMEM);
529                 extent.start = *ppos;
530                 extent.end = *ppos + count;
531                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
532                        inode->i_ino, extent.start, extent.end);
533
534                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
535                                   &extent, sizeof(extent), LCK_PW, &flags,
536                                   ll_lock_callback, inode, sizeof(*inode),
537                                   lockhs);
538                 if (err != ELDLM_OK) {
539                         CERROR("lock enqueue: err: %d\n", err);
540                         GOTO(out_free, retval = err);
541                 }
542         }
543
544         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
545                inode->i_ino, (long)count, *ppos);
546
547         retval = generic_file_write(file, buf, count, ppos);
548
549         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
550             sbi->ll_flags & LL_SBI_NOLCK) {
551                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
552                 if (err != ELDLM_OK) {
553                         CERROR("lock cancel: err: %d\n", err);
554                         GOTO(out_free, retval = err);
555                 }
556         }
557
558         EXIT;
559  out_free:
560         if (lockhs)
561                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
562
563  out_eof:
564         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
565                 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
566                 if (err && !retval)
567                         retval = err;
568         }
569
570         return retval;
571 }
572
573 static int ll_lov_setstripe(struct inode *inode, struct file *file,
574                             struct lov_user_md *lum)
575 {
576         struct ll_inode_info *lli = ll_i2info(inode);
577         struct lov_stripe_md *lsm;
578         int size = ll_mds_easize(inode->i_sb);
579         int rc;
580
581         rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
582         if (rc)
583                 RETURN(rc);
584
585         down(&lli->lli_open_sem);
586         if (lli->lli_smd) {
587                 CERROR("striping data already set for %d\n", inode->i_ino);
588                 GOTO(out_lov_up, rc = -EPERM);
589         }
590
591         OBD_ALLOC(lli->lli_smd, size);
592         if (!lli->lli_smd)
593                 GOTO(out_lov_up, rc = -ENOMEM);
594
595         lsm = lli->lli_smd;
596         lsm->lsm_magic = LOV_MAGIC;
597         lsm->lsm_stripe_size = lum->lum_stripe_size;
598         lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
599         lsm->lsm_stripe_offset = lum->lum_stripe_offset;
600         lsm->lsm_stripe_count = lum->lum_stripe_count;
601         lsm->lsm_mds_easize = size;
602
603         file->f_flags &= ~O_LOV_DELAY_CREATE;
604         rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
605         if (rc)
606                 OBD_FREE(lli->lli_smd, size);
607         else
608                 rc = ll_file_open(inode, file);
609 out_lov_up:
610         up(&lli->lli_open_sem);
611         return rc;
612 }
613
614 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
615 {
616         struct lov_user_md lum;
617         struct lov_user_md *lump;
618         struct ll_inode_info *lli = ll_i2info(inode);
619         struct lov_stripe_md *lsm = lli->lli_smd;
620         struct lov_user_oinfo *luoip;
621         struct lov_oinfo *loip;
622         int count, len, i, rc;
623
624         rc = copy_from_user(&lum, (void *)arg, sizeof(lum));
625         if (rc)
626                 RETURN(rc);
627
628         if ((count = lsm->lsm_stripe_count) == 0)
629                 count = 1;
630
631         if (lum.lum_stripe_count < count)
632                 RETURN(-EINVAL);
633
634         len = sizeof(*lump) + count * sizeof(*luoip);
635
636         rc = verify_area(VERIFY_WRITE, (void *)arg, len);
637         if (rc)
638                 RETURN(rc);
639
640         lump = (struct lov_user_md *)arg;
641         lump->lum_stripe_count = count;
642         luoip = lump->lum_luoinfo;
643
644         if (lsm->lsm_stripe_count == 0) {
645                 lump->lum_stripe_size = 0;
646                 lump->lum_stripe_pattern = 0;
647                 lump->lum_stripe_offset = 0;
648                 luoip->luo_idx = 0;
649                 luoip->luo_id = lsm->lsm_object_id;
650         } else {
651                 lump->lum_stripe_size = lsm->lsm_stripe_size;
652                 lump->lum_stripe_pattern = lsm->lsm_stripe_pattern;
653                 lump->lum_stripe_offset = lsm->lsm_stripe_offset;
654
655                 loip = lsm->lsm_oinfo;
656                 for (i = 0; i < count; i++, luoip++, loip++) {
657                         luoip->luo_idx = loip->loi_ost_idx;
658                         luoip->luo_id = loip->loi_id;
659                 }
660         }
661
662         RETURN(0);
663 }
664
665 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
666                   unsigned long arg)
667 {
668         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
669         struct lustre_handle *conn;
670         int flags;
671
672         switch(cmd) {
673         case LL_IOC_GETFLAGS:
674                 /* Get the current value of the file flags */
675                 return put_user(fd->fd_flags, (int *)arg);
676         case LL_IOC_SETFLAGS:
677         case LL_IOC_CLRFLAGS:
678                 /* Set or clear specific file flags */
679                 /* XXX This probably needs checks to ensure the flags are
680                  *     not abused, and to handle any flag side effects.
681                  */
682                 if (get_user(flags, (int *) arg))
683                         return -EFAULT;
684
685                 if (cmd == LL_IOC_SETFLAGS)
686                         fd->fd_flags |= flags;
687                 else
688                         fd->fd_flags &= ~flags;
689                 return 0;
690         case LL_IOC_LOV_SETSTRIPE:
691                 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
692         case LL_IOC_LOV_GETSTRIPE:
693                 return ll_lov_getstripe(inode, arg);
694
695         /* We need to special case any other ioctls we want to handle,
696          * to send them to the MDS/OST as appropriate and to properly
697          * network encode the arg field.
698         case EXT2_IOC_GETFLAGS:
699         case EXT2_IOC_SETFLAGS:
700         case EXT2_IOC_GETVERSION_OLD:
701         case EXT2_IOC_GETVERSION_NEW:
702         case EXT2_IOC_SETVERSION_OLD:
703         case EXT2_IOC_SETVERSION_NEW:
704         */
705         default:
706                 conn = ll_i2obdconn(inode);
707                 return obd_iocontrol(cmd, conn, 0, NULL, (void *)arg);
708         }
709 }
710
711 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
712 {
713         struct inode *inode = file->f_dentry->d_inode;
714         long long retval;
715         ENTRY;
716
717         switch (origin) {
718         case 2: {
719                 struct ll_inode_info *lli = ll_i2info(inode);
720
721                 retval = ll_file_size(inode, lli->lli_smd);
722                 if (retval)
723                         RETURN(retval);
724
725                 offset += inode->i_size;
726                 break;
727         }
728         case 1:
729                 offset += file->f_pos;
730         }
731         retval = -EINVAL;
732         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
733                 if (offset != file->f_pos) {
734                         file->f_pos = offset;
735 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
736                         file->f_reada = 0;
737 #endif
738                         file->f_version = ++event;
739                 }
740                 retval = offset;
741         }
742         RETURN(retval);
743 }
744
745 /* XXX this does not need to do anything for data, it _does_ need to
746    call setattr */
747 int ll_fsync(struct file *file, struct dentry *dentry, int data)
748 {
749         return 0;
750 }
751
752
753
754 static int ll_inode_revalidate(struct dentry *dentry)
755 {
756         struct inode *inode = dentry->d_inode;
757         struct lov_stripe_md *lsm;
758         ENTRY;
759
760         if (!inode)
761                 RETURN(0);
762
763         lsm = ll_i2info(inode)->lli_smd;
764         if (!lsm)       /* object not yet allocated, don't validate size */
765                 RETURN(0);
766
767         RETURN(ll_file_size(inode, lsm));
768 }
769
770 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
771 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
772                       struct kstat *stat)
773 {
774         return ll_inode_revalidate(de);
775 }
776 #endif
777
778 struct file_operations ll_file_operations = {
779         read:           ll_file_read,
780         write:          ll_file_write,
781         ioctl:          ll_file_ioctl,
782         open:           ll_file_open,
783         release:        ll_file_release,
784         mmap:           generic_file_mmap,
785         llseek:         ll_file_seek,
786         fsync:          NULL
787 };
788
789 struct inode_operations ll_file_inode_operations = {
790         setattr:    ll_setattr,
791         truncate:   ll_truncate,
792 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
793         getattr: ll_getattr,
794 #else
795         revalidate: ll_inode_revalidate,
796 #endif
797 };