Whamcloud - gitweb
Land b_recovery (other than WIP lock-replay work).
[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/obd_lov.h>      /* for lov_mds_md_size() in lov_setstripe() */
31 #include <linux/random.h>
32
33 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
34 extern int ll_setattr(struct dentry *de, struct iattr *attr);
35
36 int ll_create_objects(struct super_block *sb, obd_id id, uid_t uid, gid_t gid,
37                       struct lov_stripe_md **lsmp)
38 {
39         struct obdo *oa;
40         int rc;
41         ENTRY;
42
43         oa = obdo_alloc();
44         if (!oa)
45                 RETURN(-ENOMEM);
46
47         oa->o_mode = S_IFREG | 0600;
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_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 ll_sb_info *sbi = ll_i2sbi(inode);
64         struct ll_inode_info *lli = ll_i2info(inode);
65         struct lustre_handle *conn = ll_i2obdconn(inode);
66         struct ptlrpc_request *req = NULL;
67         struct ll_file_data *fd;
68         struct obdo *oa;
69         struct lov_stripe_md *lsm;
70         struct lov_mds_md *lmm = NULL;
71         int lmm_size = 0;
72         int rc = 0;
73         ENTRY;
74
75         LASSERT(!file->private_data);
76
77         lsm = lli->lli_smd;
78
79         /*  delayed create of object (intent created inode) */
80         /*  XXX object needs to be cleaned up if mdc_open fails */
81         /*  XXX error handling appropriate here? */
82         if (lsm == NULL) {
83                 if (file->f_flags & O_LOV_DELAY_CREATE) {
84                         CDEBUG(D_INODE, "delaying object creation\n");
85                         RETURN(0);
86                 }
87                 down(&lli->lli_open_sem);
88                 /* Check to see if we lost the race */
89                 if (!lli->lli_smd)
90                         rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0,
91                                                &lli->lli_smd);
92                 up(&lli->lli_open_sem);
93                 if (rc)
94                         RETURN(rc);
95
96                 lsm = lli->lli_smd;
97         }
98
99         /* XXX We should only send this to MDS if we just created these
100          *     objects, except we also need to handle the user-stripe case.
101          */
102         rc = obd_packmd(conn, &lmm, lli->lli_smd);
103         if (rc < 0)
104                 GOTO(out, rc);
105
106         lmm_size = rc;
107
108         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
109         if (!fd) {
110                 if (lmm)
111                         obd_free_wiremd(conn, &lmm);
112                 GOTO(out, rc = -ENOMEM);
113         }
114         memset(fd, 0, sizeof(*fd));
115
116         fd->fd_mdshandle.addr = (__u64)(unsigned long)file;
117         get_random_bytes(&fd->fd_mdshandle.cookie,
118                          sizeof(fd->fd_mdshandle.cookie));
119         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
120                       file->f_flags, lmm, lmm_size, &fd->fd_mdshandle, &req);
121         if (lmm)
122                 obd_free_wiremd(conn, &lmm);
123         fd->fd_req = req;
124
125         /* This is the "reply" refcount. */
126         ptlrpc_req_finished(req);
127         if (rc)
128                 GOTO(out_req, -abs(rc));
129         if (!fd->fd_mdshandle.addr ||
130             fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
131                 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
132                 /* XXX handle this how, abort or is it non-fatal? */
133         }
134
135         oa = obdo_alloc();
136         if (!oa)
137                 GOTO(out_mdc, rc = -EINVAL);
138
139         oa->o_id = lsm->lsm_object_id;
140         oa->o_mode = S_IFREG;
141         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
142                 OBD_MD_FLBLOCKS;
143         rc = obd_open(ll_i2obdconn(inode), oa, lsm);
144         obdo_to_inode(inode, oa, oa->o_valid & (OBD_MD_FLSIZE|OBD_MD_FLBLOCKS));
145
146         obd_oa2handle(&fd->fd_osthandle, oa);
147         obdo_free(oa);
148
149         if (rc)
150                 GOTO(out_mdc, rc = -abs(rc));
151
152         atomic_inc(&lli->lli_open_count);
153
154         file->private_data = fd;
155
156         RETURN(0);
157 out_mdc:
158         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
159                   S_IFREG, &fd->fd_mdshandle, &req);
160 out_req:
161         ptlrpc_req_finished(req); /* once for an early "commit" */
162 //out_fd:
163         fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
164         kmem_cache_free(ll_file_data_slab, fd);
165 out:
166         return rc;
167 }
168
169 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
170                  int mode, struct lustre_handle **lockhs_p)
171 {
172         struct ll_sb_info *sbi = ll_i2sbi(inode);
173         struct ldlm_extent extent;
174         struct lustre_handle *lockhs = NULL;
175         int rc, flags = 0, stripe_count;
176         ENTRY;
177
178         if (sbi->ll_flags & LL_SBI_NOLCK) {
179                 *lockhs_p = NULL;
180                 RETURN(0);
181         }
182
183         stripe_count = lsm->lsm_stripe_count;
184         if (!stripe_count)
185                 stripe_count = 1;
186
187         OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
188         if (lockhs == NULL)
189                 RETURN(-ENOMEM);
190
191         extent.start = start;
192         extent.end = OBD_OBJECT_EOF;
193
194         rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
195                          sizeof(extent), mode, &flags, ll_lock_callback,
196                          inode, sizeof(*inode), lockhs);
197         if (rc != ELDLM_OK) {
198                 CERROR("lock enqueue: %d\n", rc);
199                 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
200         } else
201                 *lockhs_p = lockhs;
202         RETURN(rc);
203 }
204
205 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
206                    struct lustre_handle *lockhs)
207 {
208         struct ll_sb_info *sbi = ll_i2sbi(inode);
209         int rc, stripe_count;
210         ENTRY;
211
212         if (sbi->ll_flags & LL_SBI_NOLCK)
213                 RETURN(0);
214
215         if (lockhs == NULL) {
216                 LBUG();
217                 RETURN(-EINVAL);
218         }
219
220         rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
221         if (rc != ELDLM_OK) {
222                 CERROR("lock cancel: %d\n", rc);
223                 LBUG();
224         }
225
226         stripe_count = lsm->lsm_stripe_count;
227         if (!stripe_count)
228                 stripe_count = 1;
229
230         OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
231         RETURN(rc);
232 }
233
234 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
235 {
236         struct ll_sb_info *sbi = ll_i2sbi(inode);
237         struct lustre_handle *lockhs;
238         struct obdo oa;
239         int err, rc;
240         ENTRY;
241
242         LASSERT(lsm);
243         LASSERT(sbi);
244
245         rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
246         if (rc != ELDLM_OK) {
247                 CERROR("lock enqueue: %d\n", rc);
248                 RETURN(rc);
249         }
250
251         memset(&oa, 0, sizeof oa);
252         oa.o_id = lsm->lsm_object_id;
253         oa.o_mode = S_IFREG;
254         oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
255         rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
256         if (!rc)
257                 obdo_to_inode(inode, &oa,
258                               oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
259
260         err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
261         if (err != ELDLM_OK) {
262                 CERROR("lock cancel: %d\n", err);
263                 LBUG();
264         }
265         RETURN(rc);
266 }
267
268 static int ll_file_release(struct inode *inode, struct file *file)
269 {
270         struct ptlrpc_request *req = NULL;
271         struct ll_file_data *fd;
272         struct obdo oa;
273         struct ll_sb_info *sbi = ll_i2sbi(inode);
274         struct ll_inode_info *lli = ll_i2info(inode);
275         struct lov_stripe_md *lsm = lli->lli_smd;
276         int rc, rc2;
277
278         ENTRY;
279
280         fd = (struct ll_file_data *)file->private_data;
281         if (!fd) {
282                 LASSERT(file->f_flags & O_LOV_DELAY_CREATE);
283                 GOTO(out, rc = 0);
284         }
285
286         memset(&oa, 0, sizeof(oa));
287         oa.o_id = lsm->lsm_object_id;
288         oa.o_mode = S_IFREG;
289         oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
290         obd_handle2oa(&oa, &fd->fd_osthandle);
291         rc = obd_close(ll_i2obdconn(inode), &oa, lsm);
292         if (rc)
293                 GOTO(out_mdc, rc = -abs(rc));
294
295 #if 0
296 #error "This should only be done on the node that already has the EOF lock"
297 #error "and only in the case where the file size actually changed.  For now"
298 #error "we don't care about the size on the MDS, since we never use it (the"
299 #error "OST always has the authoritative size and we don't even use the MDS."
300         /* If this fails and we goto out_fd, the file size on the MDS is out of
301          * date.  Is that a big deal? */
302         if (file->f_mode & FMODE_WRITE) {
303                 struct lustre_handle *lockhs;
304
305                 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
306                 if (rc)
307                         GOTO(out_mdc, -abs(rc));
308
309                 oa.o_id = lsm->lsm_object_id;
310                 oa.o_mode = S_IFREG;
311                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
312                         OBD_MD_FLBLOCKS;
313                 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
314                 if (!rc) {
315                         struct iattr attr;
316                         attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
317                                          ATTR_SIZE);
318                         attr.ia_mtime = inode->i_mtime;
319                         attr.ia_ctime = inode->i_ctime;
320                         attr.ia_atime = inode->i_atime;
321                         attr.ia_size = oa.o_size;
322
323                         inode->i_blocks = oa.o_blocks;
324
325                         /* XXX: this introduces a small race that we should
326                          * evaluate */
327                         rc = ll_inode_setattr(inode, &attr, 0);
328                 }
329                 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
330                 if (rc2) {
331                         CERROR("lock cancel: %d\n", rc);
332                         LBUG();
333                         if (!rc)
334                                 rc = rc2;
335                 }
336         }
337 #endif
338
339 out_mdc:
340         rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
341                         S_IFREG, &fd->fd_mdshandle, &req);
342         ptlrpc_req_finished(req);
343         if (rc2) {
344                 if (!rc)
345                         rc = -abs(rc2);
346                 GOTO(out_fd, rc);
347         }
348         DEBUG_REQ(D_HA, fd->fd_req, "matched open for this close: ");
349         ptlrpc_req_finished(fd->fd_req);
350
351         if (atomic_dec_and_test(&lli->lli_open_count)) {
352                 CDEBUG(D_INFO, "last close, cancelling unused locks\n");
353                 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
354                 if (rc)
355                         CERROR("obd_cancel_unused: %d\n", rc);
356         } else {
357                 CDEBUG(D_INFO, "not last close, not cancelling unused locks\n");
358         }
359
360         EXIT;
361
362 out_fd:
363         fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
364         file->private_data = NULL;
365         kmem_cache_free(ll_file_data_slab, fd);
366 out:
367         return rc;
368 }
369
370 static inline void ll_remove_suid(struct inode *inode)
371 {
372         unsigned int mode;
373
374         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
375         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
376
377         /* was any of the uid bits set? */
378         mode &= inode->i_mode;
379         if (mode && !capable(CAP_FSETID)) {
380                 inode->i_mode &= ~mode;
381                 // XXX careful here - we cannot change the size
382         }
383 }
384
385 static void ll_update_atime(struct inode *inode)
386 {
387         struct iattr attr;
388
389         attr.ia_atime = CURRENT_TIME;
390         attr.ia_valid = ATTR_ATIME;
391
392         if (inode->i_atime == attr.ia_atime) return;
393         if (IS_RDONLY(inode)) return;
394         if (IS_NOATIME(inode)) return;
395
396         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
397         ll_inode_setattr(inode, &attr, 0);
398 }
399
400 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
401                      void *data, __u32 data_len, int flag)
402 {
403         struct inode *inode = data;
404         struct lustre_handle lockh;
405         int rc;
406         ENTRY;
407
408         if (data_len != sizeof(struct inode))
409                 LBUG();
410
411         if (inode == NULL)
412                 LBUG();
413
414         switch (flag) {
415         case LDLM_CB_BLOCKING:
416                 ldlm_lock2handle(lock, &lockh);
417                 rc = ldlm_cli_cancel(&lockh);
418                 if (rc != ELDLM_OK)
419                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
420                 break;
421         case LDLM_CB_CANCELING:
422                 CDEBUG(D_INODE, "invalidating obdo/inode %lu\n", inode->i_ino);
423                 /* FIXME: do something better than throwing away everything */
424                 //down(&inode->i_sem);
425                 ll_invalidate_inode_pages(inode);
426                 //up(&inode->i_sem);
427                 break;
428         default:
429                 LBUG();
430         }
431
432         RETURN(0);
433 }
434
435 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
436                             loff_t *ppos)
437 {
438         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
439         struct inode *inode = filp->f_dentry->d_inode;
440         struct ll_sb_info *sbi = ll_i2sbi(inode);
441         struct lustre_handle *lockhs = NULL;
442         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
443         int flags = 0;
444         ldlm_error_t err;
445         ssize_t retval;
446         ENTRY;
447
448         /* If we don't refresh the file size, generic_file_read may not even
449          * call us */
450         retval = ll_file_size(inode, lsm);
451         if (retval < 0) {
452                 CERROR("ll_file_size: %d\n", retval);
453                 RETURN(retval);
454         }
455
456         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
457             !(sbi->ll_flags & LL_SBI_NOLCK)) {
458                 struct ldlm_extent extent;
459                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
460                 if (!lockhs)
461                         RETURN(-ENOMEM);
462
463                 extent.start = *ppos;
464                 extent.end = *ppos + count;
465                 CDEBUG(D_INFO, "Locking inode %lu, start "LPU64" end "LPU64"\n",
466                        inode->i_ino, extent.start, extent.end);
467
468                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
469                                   &extent, sizeof(extent), LCK_PR, &flags,
470                                   ll_lock_callback, inode, sizeof(*inode),
471                                   lockhs);
472                 if (err != ELDLM_OK) {
473                         OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
474                         CERROR("lock enqueue: err: %d\n", err);
475                         RETURN(err);
476                 }
477         }
478
479         CDEBUG(D_INFO, "Reading inode %lu, %d bytes, offset %Ld\n",
480                inode->i_ino, count, *ppos);
481         retval = generic_file_read(filp, buf, count, ppos);
482
483         if (retval > 0)
484                 ll_update_atime(inode);
485
486         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
487             !(sbi->ll_flags & LL_SBI_NOLCK)) {
488                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
489                 if (err != ELDLM_OK) {
490                         CERROR("lock cancel: err: %d\n", err);
491                         retval = err;
492                 }
493         }
494
495         if (lockhs)
496                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
497         RETURN(retval);
498 }
499
500 /*
501  * Write to a file (through the page cache).
502  */
503 static ssize_t
504 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
505 {
506         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
507         struct inode *inode = file->f_dentry->d_inode;
508         struct ll_sb_info *sbi = ll_i2sbi(inode);
509         struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
510         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
511         int flags = 0;
512         ldlm_error_t err;
513         ssize_t retval;
514         ENTRY;
515
516         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
517                 struct obdo *oa;
518
519                 oa = obdo_alloc();
520                 if (!oa)
521                         RETURN(-ENOMEM);
522
523                 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
524                 if (err) {
525                         obdo_free(oa);
526                         RETURN(err);
527                 }
528
529                 oa->o_id = lsm->lsm_object_id;
530                 oa->o_mode = inode->i_mode;
531                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
532                         OBD_MD_FLBLOCKS;
533                 obd_handle2oa(oa, &fd->fd_osthandle);
534                 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
535                 if (retval) {
536                         obdo_free(oa);
537                         GOTO(out_eof, retval);
538                 }
539
540                 *ppos = oa->o_size;
541                 obdo_to_inode(inode, oa, oa->o_valid);
542                 obdo_free(oa);
543         }
544
545         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
546             !(sbi->ll_flags & LL_SBI_NOLCK)) {
547                 struct ldlm_extent extent;
548                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
549                 if (!lockhs)
550                         GOTO(out_eof, retval = -ENOMEM);
551                 extent.start = *ppos;
552                 extent.end = *ppos + count;
553                 CDEBUG(D_INFO, "Locking inode %lu, start "LPU64" end "LPU64"\n",
554                        inode->i_ino, extent.start, extent.end);
555
556                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
557                                   &extent, sizeof(extent), LCK_PW, &flags,
558                                   ll_lock_callback, inode, sizeof(*inode),
559                                   lockhs);
560                 if (err != ELDLM_OK) {
561                         CERROR("lock enqueue: err: %d\n", err);
562                         GOTO(out_free, retval = err);
563                 }
564         }
565
566         CDEBUG(D_INFO, "Writing inode %lu, %ld bytes, offset "LPD64"\n",
567                inode->i_ino, (long)count, *ppos);
568
569         retval = generic_file_write(file, buf, count, ppos);
570
571         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
572             sbi->ll_flags & LL_SBI_NOLCK) {
573                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
574                 if (err != ELDLM_OK) {
575                         CERROR("lock cancel: err: %d\n", err);
576                         GOTO(out_free, retval = err);
577                 }
578         }
579
580         EXIT;
581  out_free:
582         if (lockhs)
583                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
584
585  out_eof:
586         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
587                 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
588                 if (err && !retval)
589                         retval = err;
590         }
591
592         return retval;
593 }
594
595 /* Retrieve object striping information.
596  *
597  * @arg is a pointer to a user struct with one or more of the fields set to
598  * indicate the application preference: lmm_stripe_count, lmm_stripe_size,
599  * lmm_stripe_offset, and lmm_stripe_pattern.  lmm_magic must be LOV_MAGIC.
600  */
601 static int ll_lov_setstripe(struct inode *inode, struct file *file,
602                             unsigned long arg)
603 {
604         struct ll_inode_info *lli = ll_i2info(inode);
605         struct lov_mds_md *lmm = NULL, *lmmu = (void *)arg;
606         struct lustre_handle *conn = ll_i2obdconn(inode);
607         int rc;
608
609         rc = obd_alloc_wiremd(conn, &lmm);
610         if (rc < 0)
611                 RETURN(rc);
612
613         rc = copy_from_user(lmm, lmmu, sizeof(*lmm));
614         if (rc)
615                 GOTO(out_free, rc = -EFAULT);
616
617         if (lmm->lmm_magic != LOV_MAGIC) {
618                 CERROR("bad LOV magic %X\n", lmm->lmm_magic);
619                 GOTO(out_free, rc = -EINVAL);
620         }
621
622         down(&lli->lli_open_sem);
623         if (lli->lli_smd) {
624                 CERROR("striping data already set for %lu\n", inode->i_ino);
625                 GOTO(out_lov_up, rc = -EPERM);
626         }
627         rc = obd_unpackmd(conn, &lli->lli_smd, lmm);
628         if (rc < 0) {
629                 CERROR("error setting LOV striping on %lu: rc = %d\n",
630                        inode->i_ino, rc);
631                 GOTO(out_lov_up, rc);
632         }
633
634         rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lli->lli_smd);
635         if (rc) {
636                 obd_free_memmd(conn, &lli->lli_smd);
637         } else {
638                 file->f_flags &= ~O_LOV_DELAY_CREATE;
639                 rc = ll_file_open(inode, file);
640         }
641 out_lov_up:
642         up(&lli->lli_open_sem);
643 out_free:
644         obd_free_wiremd(conn, &lmm);
645         return rc;
646 }
647
648 /* Retrieve object striping information.
649  *
650  * @arg is a pointer to a user struct with lmm_ost_count indicating
651  * the maximum number of OST indices which will fit in the user buffer.
652  * lmm_magic must be LOV_MAGIC.
653  */
654 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
655 {
656         struct lov_mds_md lmm, *lmmu = (void *)arg, *lmmk = NULL;
657         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
658         struct lustre_handle *conn = ll_i2obdconn(inode);
659         int ost_count, rc, lmm_size;
660
661         if (!lsm)
662                 RETURN(-ENODATA);
663
664         rc = copy_from_user(&lmm, lmmu, sizeof(lmm));
665         if (rc)
666                 RETURN(-EFAULT);
667
668         if (lmm.lmm_magic != LOV_MAGIC)
669                 RETURN(-EINVAL);
670
671         if (lsm->lsm_stripe_count == 0)
672                 ost_count = 1;
673         else {
674                 struct obd_device *obd = class_conn2obd(conn);
675                 struct lov_obd *lov = &obd->u.lov;
676                 ost_count = lov->desc.ld_tgt_count;
677         }
678
679         /* XXX we _could_ check if indices > user lmm_ost_count are zero */
680         if (lmm.lmm_ost_count < ost_count)
681                 RETURN(-EOVERFLOW);
682
683         rc = obd_packmd(conn, &lmmk, lsm);
684         if (rc < 0)
685                 RETURN(rc);
686
687         lmm_size = rc;
688
689         /* LOV STACKING layering violation to make LOV/OSC return same data */
690         if (lsm->lsm_stripe_count == 0) {
691                 struct lov_object_id *loi;
692
693                 loi = (void *)lmmu + offsetof(typeof(*lmmu), lmm_objects);
694                 rc = copy_to_user(loi, &lsm->lsm_object_id, sizeof(*loi));
695                 if (rc) {
696                         lmm_size = 0;
697                         rc = -EFAULT;
698                 } else {
699                         lmmk->lmm_magic = LOV_MAGIC;
700                         lmmk->lmm_ost_count = lmmk->lmm_stripe_count = 1;
701                 }
702         }
703
704         if (lmm_size && copy_to_user(lmmu, lmmk, lmm_size))
705                 rc = -EFAULT;
706
707         obd_free_wiremd(conn, &lmmk);
708
709         RETURN(rc);
710 }
711
712 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
713                   unsigned long arg)
714 {
715         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
716         struct lustre_handle *conn;
717         int flags;
718
719         switch(cmd) {
720         case LL_IOC_GETFLAGS:
721                 /* Get the current value of the file flags */
722                 return put_user(fd->fd_flags, (int *)arg);
723         case LL_IOC_SETFLAGS:
724         case LL_IOC_CLRFLAGS:
725                 /* Set or clear specific file flags */
726                 /* XXX This probably needs checks to ensure the flags are
727                  *     not abused, and to handle any flag side effects.
728                  */
729                 if (get_user(flags, (int *) arg))
730                         return -EFAULT;
731
732                 if (cmd == LL_IOC_SETFLAGS)
733                         fd->fd_flags |= flags;
734                 else
735                         fd->fd_flags &= ~flags;
736                 return 0;
737         case LL_IOC_LOV_SETSTRIPE:
738                 return ll_lov_setstripe(inode, file, arg);
739         case LL_IOC_LOV_GETSTRIPE:
740                 return ll_lov_getstripe(inode, arg);
741
742         /* We need to special case any other ioctls we want to handle,
743          * to send them to the MDS/OST as appropriate and to properly
744          * network encode the arg field.
745         case EXT2_IOC_GETFLAGS:
746         case EXT2_IOC_SETFLAGS:
747         case EXT2_IOC_GETVERSION_OLD:
748         case EXT2_IOC_GETVERSION_NEW:
749         case EXT2_IOC_SETVERSION_OLD:
750         case EXT2_IOC_SETVERSION_NEW:
751         */
752         default:
753                 conn = ll_i2obdconn(inode);
754                 return obd_iocontrol(cmd, conn, 0, NULL, (void *)arg);
755         }
756 }
757
758 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
759 {
760         struct inode *inode = file->f_dentry->d_inode;
761         long long retval;
762         ENTRY;
763
764         switch (origin) {
765         case 2: {
766                 struct ll_inode_info *lli = ll_i2info(inode);
767
768                 retval = ll_file_size(inode, lli->lli_smd);
769                 if (retval)
770                         RETURN(retval);
771
772                 offset += inode->i_size;
773                 break;
774         }
775         case 1:
776                 offset += file->f_pos;
777         }
778         retval = -EINVAL;
779         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
780                 if (offset != file->f_pos) {
781                         file->f_pos = offset;
782 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
783                         file->f_reada = 0;
784 #endif
785                         file->f_version = ++event;
786                 }
787                 retval = offset;
788         }
789         RETURN(retval);
790 }
791
792 /* XXX this does not need to do anything for data, it _does_ need to
793    call setattr */
794 int ll_fsync(struct file *file, struct dentry *dentry, int data)
795 {
796         return 0;
797 }
798
799 static int ll_inode_revalidate(struct dentry *dentry)
800 {
801         struct inode *inode = dentry->d_inode;
802         struct lov_stripe_md *lsm;
803         ENTRY;
804
805         if (!inode) {
806                 CERROR("REPORT THIS LINE TO PETER\n");
807                 RETURN(0);
808         }
809
810         if (!ll_have_md_lock(dentry)) {
811                 struct ptlrpc_request *req = NULL;
812                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
813                 struct mds_body *body;
814                 unsigned long valid = 0;
815                 int datalen = 0;
816                 int rc;
817
818                 if (S_ISREG(inode->i_mode)) {
819                         datalen = obd_size_wiremd(&sbi->ll_osc_conn, NULL);
820                         valid |= OBD_MD_FLEASIZE;
821                 }
822                 rc = mdc_getattr(&sbi->ll_mdc_conn, inode->i_ino,
823                                  inode->i_mode, valid, datalen, &req);
824                 if (rc) {
825                         CERROR("failure %d inode "LPX64"\n", rc, inode->i_ino);
826                         ptlrpc_req_finished(req);
827                         RETURN(-abs(rc));
828                 }
829
830                 body = lustre_msg_buf(req->rq_repmsg, 0);
831                 ll_update_inode(inode, body);
832                 ptlrpc_req_finished(req);
833         }
834
835         lsm = ll_i2info(inode)->lli_smd;
836         if (!lsm)       /* object not yet allocated, don't validate size */
837                 RETURN(0);
838
839         RETURN(ll_file_size(inode, lsm));
840 }
841
842 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
843 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
844                       struct kstat *stat)
845 {
846         return ll_inode_revalidate(de);
847 }
848 #endif
849
850 struct file_operations ll_file_operations = {
851         read:           ll_file_read,
852         write:          ll_file_write,
853         ioctl:          ll_file_ioctl,
854         open:           ll_file_open,
855         release:        ll_file_release,
856         mmap:           generic_file_mmap,
857         llseek:         ll_file_seek,
858         fsync:          NULL
859 };
860
861 struct inode_operations ll_file_inode_operations = {
862         setattr:    ll_setattr,
863         truncate:   ll_truncate,
864 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
865         getattr: ll_getattr,
866 #else
867         revalidate: ll_inode_revalidate,
868 #endif
869 };