Whamcloud - gitweb
Update the file size _before_ taking the read lock, or you will deadlock in
[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 we don't refresh the file size, generic_file_read may not even
435          * call us */
436         retval = ll_file_size(inode, lsm);
437         if (retval < 0) {
438                 CERROR("ll_file_size: %d\n", retval);
439                 RETURN(retval);
440         }
441
442         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
443             !(sbi->ll_flags & LL_SBI_NOLCK)) {
444                 struct ldlm_extent extent;
445                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
446                 if (!lockhs)
447                         RETURN(-ENOMEM);
448
449                 extent.start = *ppos;
450                 extent.end = *ppos + count;
451                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
452                        inode->i_ino, extent.start, extent.end);
453
454                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
455                                   &extent, sizeof(extent), LCK_PR, &flags,
456                                   ll_lock_callback, inode, sizeof(*inode),
457                                   lockhs);
458                 if (err != ELDLM_OK) {
459                         OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
460                         CERROR("lock enqueue: err: %d\n", err);
461                         RETURN(err);
462                 }
463         }
464
465         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
466                inode->i_ino, count, *ppos);
467         retval = generic_file_read(filp, buf, count, ppos);
468
469         if (retval > 0)
470                 ll_update_atime(inode);
471
472         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
473             !(sbi->ll_flags & LL_SBI_NOLCK)) {
474                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
475                 if (err != ELDLM_OK) {
476                         CERROR("lock cancel: err: %d\n", err);
477                         retval = err;
478                 }
479         }
480
481         if (lockhs)
482                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
483         RETURN(retval);
484 }
485
486 /*
487  * Write to a file (through the page cache).
488  */
489 static ssize_t
490 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
491 {
492         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
493         struct inode *inode = file->f_dentry->d_inode;
494         struct ll_sb_info *sbi = ll_i2sbi(inode);
495         struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
496         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
497         int flags = 0;
498         ldlm_error_t err;
499         ssize_t retval;
500         ENTRY;
501
502         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
503                 struct obdo *oa;
504
505                 oa = obdo_alloc();
506                 if (!oa)
507                         RETURN(-ENOMEM);
508
509                 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
510                 if (err) {
511                         obdo_free(oa);
512                         RETURN(err);
513                 }
514
515                 oa->o_id = lsm->lsm_object_id;
516                 oa->o_mode = inode->i_mode;
517                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
518                         OBD_MD_FLBLOCKS;
519                 obd_handle2oa(oa, &fd->fd_osthandle);
520                 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
521                 if (retval) {
522                         obdo_free(oa);
523                         GOTO(out_eof, retval);
524                 }
525
526                 *ppos = oa->o_size;
527                 obdo_to_inode(inode, oa, oa->o_valid);
528                 obdo_free(oa);
529         }
530
531         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
532             !(sbi->ll_flags & LL_SBI_NOLCK)) {
533                 struct ldlm_extent extent;
534                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
535                 if (!lockhs)
536                         GOTO(out_eof, retval = -ENOMEM);
537                 extent.start = *ppos;
538                 extent.end = *ppos + count;
539                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
540                        inode->i_ino, extent.start, extent.end);
541
542                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
543                                   &extent, sizeof(extent), LCK_PW, &flags,
544                                   ll_lock_callback, inode, sizeof(*inode),
545                                   lockhs);
546                 if (err != ELDLM_OK) {
547                         CERROR("lock enqueue: err: %d\n", err);
548                         GOTO(out_free, retval = err);
549                 }
550         }
551
552         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
553                inode->i_ino, (long)count, *ppos);
554
555         retval = generic_file_write(file, buf, count, ppos);
556
557         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
558             sbi->ll_flags & LL_SBI_NOLCK) {
559                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
560                 if (err != ELDLM_OK) {
561                         CERROR("lock cancel: err: %d\n", err);
562                         GOTO(out_free, retval = err);
563                 }
564         }
565
566         EXIT;
567  out_free:
568         if (lockhs)
569                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
570
571  out_eof:
572         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
573                 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
574                 if (err && !retval)
575                         retval = err;
576         }
577
578         return retval;
579 }
580
581 static int ll_lov_setstripe(struct inode *inode, struct file *file,
582                             struct lov_user_md *lum)
583 {
584         struct ll_inode_info *lli = ll_i2info(inode);
585         struct lov_stripe_md *lsm;
586         int size = ll_mds_easize(inode->i_sb);
587         int rc;
588
589         rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
590         if (rc)
591                 RETURN(rc);
592
593         down(&lli->lli_open_sem);
594         if (lli->lli_smd) {
595                 CERROR("striping data already set for %d\n", inode->i_ino);
596                 GOTO(out_lov_up, rc = -EPERM);
597         }
598
599         OBD_ALLOC(lli->lli_smd, size);
600         if (!lli->lli_smd)
601                 GOTO(out_lov_up, rc = -ENOMEM);
602
603         lsm = lli->lli_smd;
604         lsm->lsm_magic = LOV_MAGIC;
605         lsm->lsm_stripe_size = lum->lum_stripe_size;
606         lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
607         lsm->lsm_stripe_offset = lum->lum_stripe_offset;
608         lsm->lsm_stripe_count = lum->lum_stripe_count;
609         lsm->lsm_mds_easize = size;
610
611         file->f_flags &= ~O_LOV_DELAY_CREATE;
612         rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
613         if (rc)
614                 OBD_FREE(lli->lli_smd, size);
615         else
616                 rc = ll_file_open(inode, file);
617 out_lov_up:
618         up(&lli->lli_open_sem);
619         return rc;
620 }
621
622 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
623 {
624         struct lov_user_md lum;
625         struct lov_user_md *lump;
626         struct ll_inode_info *lli = ll_i2info(inode);
627         struct lov_stripe_md *lsm = lli->lli_smd;
628         struct lov_user_oinfo *luoip;
629         struct lov_oinfo *loip;
630         int count, len, i, rc;
631
632         rc = copy_from_user(&lum, (void *)arg, sizeof(lum));
633         if (rc)
634                 RETURN(rc);
635
636         if ((count = lsm->lsm_stripe_count) == 0)
637                 count = 1;
638
639         if (lum.lum_stripe_count < count)
640                 RETURN(-EINVAL);
641
642         len = sizeof(*lump) + count * sizeof(*luoip);
643
644         rc = verify_area(VERIFY_WRITE, (void *)arg, len);
645         if (rc)
646                 RETURN(rc);
647
648         lump = (struct lov_user_md *)arg;
649         lump->lum_stripe_count = count;
650         luoip = lump->lum_luoinfo;
651
652         if (lsm->lsm_stripe_count == 0) {
653                 lump->lum_stripe_size = 0;
654                 lump->lum_stripe_pattern = 0;
655                 lump->lum_stripe_offset = 0;
656                 luoip->luo_idx = 0;
657                 luoip->luo_id = lsm->lsm_object_id;
658         } else {
659                 lump->lum_stripe_size = lsm->lsm_stripe_size;
660                 lump->lum_stripe_pattern = lsm->lsm_stripe_pattern;
661                 lump->lum_stripe_offset = lsm->lsm_stripe_offset;
662
663                 loip = lsm->lsm_oinfo;
664                 for (i = 0; i < count; i++, luoip++, loip++) {
665                         luoip->luo_idx = loip->loi_ost_idx;
666                         luoip->luo_id = loip->loi_id;
667                 }
668         }
669
670         RETURN(0);
671 }
672
673 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
674                   unsigned long arg)
675 {
676         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
677         struct lustre_handle *conn;
678         int flags;
679
680         switch(cmd) {
681         case LL_IOC_GETFLAGS:
682                 /* Get the current value of the file flags */
683                 return put_user(fd->fd_flags, (int *)arg);
684         case LL_IOC_SETFLAGS:
685         case LL_IOC_CLRFLAGS:
686                 /* Set or clear specific file flags */
687                 /* XXX This probably needs checks to ensure the flags are
688                  *     not abused, and to handle any flag side effects.
689                  */
690                 if (get_user(flags, (int *) arg))
691                         return -EFAULT;
692
693                 if (cmd == LL_IOC_SETFLAGS)
694                         fd->fd_flags |= flags;
695                 else
696                         fd->fd_flags &= ~flags;
697                 return 0;
698         case LL_IOC_LOV_SETSTRIPE:
699                 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
700         case LL_IOC_LOV_GETSTRIPE:
701                 return ll_lov_getstripe(inode, arg);
702
703         /* We need to special case any other ioctls we want to handle,
704          * to send them to the MDS/OST as appropriate and to properly
705          * network encode the arg field.
706         case EXT2_IOC_GETFLAGS:
707         case EXT2_IOC_SETFLAGS:
708         case EXT2_IOC_GETVERSION_OLD:
709         case EXT2_IOC_GETVERSION_NEW:
710         case EXT2_IOC_SETVERSION_OLD:
711         case EXT2_IOC_SETVERSION_NEW:
712         */
713         default:
714                 conn = ll_i2obdconn(inode);
715                 return obd_iocontrol(cmd, conn, 0, NULL, (void *)arg);
716         }
717 }
718
719 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
720 {
721         struct inode *inode = file->f_dentry->d_inode;
722         long long retval;
723         ENTRY;
724
725         switch (origin) {
726         case 2: {
727                 struct ll_inode_info *lli = ll_i2info(inode);
728
729                 retval = ll_file_size(inode, lli->lli_smd);
730                 if (retval)
731                         RETURN(retval);
732
733                 offset += inode->i_size;
734                 break;
735         }
736         case 1:
737                 offset += file->f_pos;
738         }
739         retval = -EINVAL;
740         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
741                 if (offset != file->f_pos) {
742                         file->f_pos = offset;
743 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
744                         file->f_reada = 0;
745 #endif
746                         file->f_version = ++event;
747                 }
748                 retval = offset;
749         }
750         RETURN(retval);
751 }
752
753 /* XXX this does not need to do anything for data, it _does_ need to
754    call setattr */
755 int ll_fsync(struct file *file, struct dentry *dentry, int data)
756 {
757         return 0;
758 }
759
760
761
762 static int ll_inode_revalidate(struct dentry *dentry)
763 {
764         struct inode *inode = dentry->d_inode;
765         struct lov_stripe_md *lsm;
766         ENTRY;
767
768         if (!inode)
769                 RETURN(0);
770
771         lsm = ll_i2info(inode)->lli_smd;
772         if (!lsm)       /* object not yet allocated, don't validate size */
773                 RETURN(0);
774
775         RETURN(ll_file_size(inode, lsm));
776 }
777
778 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
779 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
780                       struct kstat *stat)
781 {
782         return ll_inode_revalidate(de);
783 }
784 #endif
785
786 struct file_operations ll_file_operations = {
787         read:           ll_file_read,
788         write:          ll_file_write,
789         ioctl:          ll_file_ioctl,
790         open:           ll_file_open,
791         release:        ll_file_release,
792         mmap:           generic_file_mmap,
793         llseek:         ll_file_seek,
794         fsync:          NULL
795 };
796
797 struct inode_operations ll_file_inode_operations = {
798         setattr:    ll_setattr,
799         truncate:   ll_truncate,
800 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
801         getattr: ll_getattr,
802 #else
803         revalidate: ll_inode_revalidate,
804 #endif
805 };