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