Whamcloud - gitweb
Don't fail assertion if lock has waiters.
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #include <linux/version.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_ha.h>
31 #include <linux/lustre_dlm.h>
32 #include <linux/init.h>
33 #include <linux/fs.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
36
37 kmem_cache_t *ll_file_data_slab;
38
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
41 extern struct super_operations ll_super_operations;
42
43 #ifndef log2
44 #define log2(n) ffz(~(n))
45 #endif
46
47 char *ll_read_opt(const char *opt, char *data)
48 {
49         char *value;
50         char *retval;
51         ENTRY;
52
53         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
54         if (strncmp(opt, data, strlen(opt)))
55                 RETURN(NULL);
56         if ((value = strchr(data, '=')) == NULL)
57                 RETURN(NULL);
58
59         value++;
60         OBD_ALLOC(retval, strlen(value) + 1);
61         if (!retval) {
62                 CERROR("out of memory!\n");
63                 RETURN(NULL);
64         }
65
66         memcpy(retval, value, strlen(value)+1);
67         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
68         RETURN(retval);
69 }
70
71 int ll_set_opt(const char *opt, char *data, int fl)
72 {
73         ENTRY;
74
75         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
76         if (strncmp(opt, data, strlen(opt)))
77                 RETURN(0);
78         else
79                 RETURN(fl);
80 }
81
82 void ll_options(char *options, char **ost, char **mds, int *flags)
83 {
84         char *this_char;
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
86         char *opt_ptr = options;
87 #endif
88         ENTRY;
89
90         if (!options) {
91                 EXIT;
92                 return;
93         }
94
95 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
96         for (this_char = strtok (options, ",");
97              this_char != NULL;
98              this_char = strtok (NULL, ",")) {
99 #else
100         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
101 #endif
102                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
103                 if ((!*ost && (*ost = ll_read_opt("osc", this_char)))||
104                     (!*mds && (*mds = ll_read_opt("mdc", this_char)))||
105                     (!(*flags & LL_SBI_NOLCK) &&
106                      ((*flags) = (*flags) |
107                       ll_set_opt("nolock", this_char, LL_SBI_NOLCK))))
108                         continue;
109         }
110         EXIT;
111 }
112
113 void ll_lli_init(struct ll_inode_info *lli)
114 {
115         sema_init(&lli->lli_open_sem, 1);
116         spin_lock_init(&lli->lli_read_extent_lock);
117         INIT_LIST_HEAD(&lli->lli_read_extents);
118         lli->lli_flags = 0;
119         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
120 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
121         ll_lldo_init(&lli->lli_dirty);
122         spin_lock_init(&lli->lli_pg_lock);
123         INIT_LIST_HEAD(&lli->lli_lc_item);
124         plist_init(&lli->lli_pl_read);
125         plist_init(&lli->lli_pl_write);
126         atomic_set(&lli->lli_in_writepages, 0);
127 #endif
128 }
129
130 int ll_fill_super(struct super_block *sb, void *data, int silent)
131 {
132         struct inode *root = 0;
133         struct obd_device *obd;
134         struct ll_sb_info *sbi;
135         char *osc = NULL;
136         char *mdc = NULL;
137         int err;
138         struct ll_fid rootfid;
139         struct obd_statfs osfs;
140         struct ptlrpc_request *request = NULL;
141         struct ptlrpc_connection *mdc_conn;
142         struct lustre_md md;
143         class_uuid_t uuid;
144
145         ENTRY;
146
147         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
148         OBD_ALLOC(sbi, sizeof(*sbi));
149         if (!sbi)
150                 RETURN(-ENOMEM);
151
152         INIT_LIST_HEAD(&sbi->ll_conn_chain);
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
154         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
155         sb->u.generic_sbp = sbi;
156 #else
157         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
158         spin_lock_init(&sbi->ll_iostats.fis_lock);
159         ll_s2sbi(sb) = sbi;
160 #endif
161         generate_random_uuid(uuid);
162         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
163
164         ll_options(data, &osc, &mdc, &sbi->ll_flags);
165
166         if (!osc) {
167                 CERROR("no osc\n");
168                 GOTO(out_free, err = -EINVAL);
169         }
170
171         if (!mdc) {
172                 CERROR("no mdc\n");
173                 GOTO(out_free, err = -EINVAL);
174         }
175
176         obd = class_name2obd(mdc);
177         if (!obd) {
178                 CERROR("MDC %s: not setup or attached\n", mdc);
179                 GOTO(out_free, err = -EINVAL);
180         }
181
182         err = obd_connect(&sbi->ll_mdc_conn, obd, &sbi->ll_sb_uuid);
183         if (err) {
184                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
185                 GOTO(out_free, err);
186         }
187
188         err = obd_statfs(obd, &osfs, jiffies - HZ);
189         if (err)
190                 GOTO(out_mdc, err);
191
192         LASSERT(osfs.os_bsize);
193         sb->s_blocksize = osfs.os_bsize;
194         sb->s_blocksize_bits = log2(osfs.os_bsize);
195         sb->s_magic = LL_SUPER_MAGIC;
196         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
197
198         mdc_conn = sbi2mdc(sbi)->cl_import->imp_connection;
199
200         obd = class_name2obd(osc);
201         if (!obd) {
202                 CERROR("OSC %s: not setup or attached\n", osc);
203                 GOTO(out_mdc, err);
204         }
205
206         err = obd_connect(&sbi->ll_osc_conn, obd, &sbi->ll_sb_uuid);
207         if (err) {
208                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
209                 GOTO(out_mdc, err);
210         }
211
212         err = mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
213         if (err) {
214                 CERROR("cannot mds_connect: rc = %d\n", err);
215                 GOTO(out_osc, err);
216         }
217         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
218         sbi->ll_rootino = rootfid.id;
219
220         sb->s_op = &ll_super_operations;
221
222         /* make root inode 
223          * XXX: move this to after cbd setup? */
224         err = mdc_getattr(&sbi->ll_mdc_conn, &rootfid,
225                           OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
226         if (err) {
227                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
228                 GOTO(out_osc, err);
229         }
230
231         /* initialize committed transaction callback daemon */
232         spin_lock_init(&sbi->ll_commitcbd_lock);
233         init_waitqueue_head(&sbi->ll_commitcbd_waitq);
234         init_waitqueue_head(&sbi->ll_commitcbd_ctl_waitq);
235         sbi->ll_commitcbd_flags = 0;
236         err = ll_commitcbd_setup(sbi);
237         if (err) {
238                 CERROR("failed to start commit callback daemon: rc = %d\n",err);
239                 ptlrpc_req_finished (request);
240                 GOTO(out_lliod, err);
241         }
242
243         err = mdc_req2lustre_md(request, 0, &sbi->ll_osc_conn, &md);
244         if (err) {
245                 CERROR("failed to understand root inode md: rc = %d\n",err);
246                 ptlrpc_req_finished (request);
247                 GOTO(out_lliod, err);
248         }
249
250         LASSERT(sbi->ll_rootino != 0);
251 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
252         root = iget4(sb, sbi->ll_rootino, NULL, &md);
253 #else
254         root = ll_iget(sb, sbi->ll_rootino, &md);
255 #endif
256
257         ptlrpc_req_finished(request);
258
259         if (root == NULL || is_bad_inode(root)) {
260                 /* XXX might need iput() for bad inode */
261                 CERROR("lustre_lite: bad iget4 for root\n");
262                 GOTO(out_cbd, err = -EBADF);
263         }
264
265 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
266         /* initialize the pagecache writeback thread */
267         err = lliod_start(sbi, root);
268         if (err) {
269                 CERROR("failed to start lliod: rc = %d\n",err);
270                 GOTO(out_root, sb = NULL);
271         }
272 #endif
273         sb->s_root = d_alloc_root(root);
274
275         if (proc_lustre_fs_root) {
276                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
277                                                   osc, mdc);
278                 if (err < 0)
279                         CERROR("could not register mount in /proc/lustre");
280         }
281
282 out_dev:
283         if (mdc)
284                 OBD_FREE(mdc, strlen(mdc) + 1);
285         if (osc)
286                 OBD_FREE(osc, strlen(osc) + 1);
287
288         RETURN(err);
289
290 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
291 out_root:
292         iput(root);
293 #endif
294 out_cbd:
295         ll_commitcbd_cleanup(sbi);
296 out_lliod:
297 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
298         lliod_stop(sbi);
299 #endif
300 out_osc:
301         obd_disconnect(&sbi->ll_osc_conn, 0);
302 out_mdc:
303         obd_disconnect(&sbi->ll_mdc_conn, 0);
304 out_free:
305         lprocfs_unregister_mountpoint(sbi);
306         OBD_FREE(sbi, sizeof(*sbi));
307
308         goto out_dev;
309 } /* ll_read_super */
310
311 void ll_put_super(struct super_block *sb)
312 {
313         struct ll_sb_info *sbi = ll_s2sbi(sb);
314 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
315         struct obd_device *obd = class_conn2obd(&sbi->ll_mdc_conn);
316         struct list_head *tmp, *next;
317 #else
318         struct hlist_node *tmp, *next;
319 #endif
320         struct ll_fid rootfid;
321         ENTRY;
322
323         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
324         list_del(&sbi->ll_conn_chain);
325         ll_commitcbd_cleanup(sbi);
326 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
327         lliod_stop(sbi);
328 #endif
329         obd_disconnect(&sbi->ll_osc_conn, 0);
330
331         /* NULL request to force sync on the MDS, and get the last_committed
332          * value to flush remaining RPCs from the sending queue on client.
333          *
334          * XXX This should be an mdc_sync() call to sync the whole MDS fs,
335          *     which we can call for other reasons as well.
336          */
337 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
338         if (!obd->obd_no_recov)
339 #endif
340                 mdc_getstatus(&sbi->ll_mdc_conn, &rootfid);
341
342         lprocfs_unregister_mountpoint(sbi);
343         if (sbi->ll_proc_root) {
344                 lprocfs_remove(sbi->ll_proc_root);
345                 sbi->ll_proc_root = NULL;
346         }
347
348         obd_disconnect(&sbi->ll_mdc_conn, 0);
349
350         spin_lock(&dcache_lock);
351 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
352         list_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
353                 struct dentry *dentry = list_entry(tmp, struct dentry, d_hash);
354                 shrink_dcache_parent(dentry);
355         }
356 #else
357         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
358                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
359                 shrink_dcache_parent(dentry);
360         }
361 #endif
362         spin_unlock(&dcache_lock);
363
364         OBD_FREE(sbi, sizeof(*sbi));
365
366         EXIT;
367 } /* ll_put_super */
368
369 void ll_clear_inode(struct inode *inode)
370 {
371         struct ll_sb_info *sbi = ll_i2sbi(inode);
372         struct ll_inode_info *lli = ll_i2info(inode);
373         int rc;
374         ENTRY;
375
376         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
377                inode->i_generation, inode);
378         rc = ll_mdc_cancel_unused(&sbi->ll_mdc_conn, inode,
379                                   LDLM_FL_WARN | LDLM_FL_NO_CALLBACK, inode);
380         if (rc < 0) {
381                 CERROR("ll_mdc_cancel_unused: %d\n", rc);
382                 /* XXX FIXME do something dramatic */
383         }
384
385         if (atomic_read(&inode->i_count) != 0)
386                 CERROR("clearing in-use inode %lu: count = %d\n",
387                        inode->i_ino, atomic_read(&inode->i_count));
388
389         if (lli->lli_smd) {
390                 rc = obd_cancel_unused(&sbi->ll_osc_conn, lli->lli_smd,
391                                        LDLM_FL_WARN, inode);
392                 if (rc < 0) {
393                         CERROR("obd_cancel_unused: %d\n", rc);
394                         /* XXX FIXME do something dramatic */
395                 }
396                 obd_free_memmd(&sbi->ll_osc_conn, &lli->lli_smd);
397                 lli->lli_smd = NULL;
398         }
399
400         if (lli->lli_symlink_name) {
401                 OBD_FREE(lli->lli_symlink_name,
402                          strlen(lli->lli_symlink_name) + 1);
403                 lli->lli_symlink_name = NULL;
404         }
405
406         EXIT;
407 }
408
409 #if 0
410 static void ll_delete_inode(struct inode *inode)
411 {
412         ENTRY;
413         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu(%p)\n", inode->i_ino, inode);
414         if (S_ISREG(inode->i_mode)) {
415                 int err;
416                 struct obdo *oa;
417                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
418
419                 /* mcreate with no open */
420                 if (!lsm)
421                         GOTO(out, 0);
422
423                 if (lsm->lsm_object_id == 0) {
424                         CERROR("This really happens\n");
425                         /* No obdo was ever created */
426                         GOTO(out, 0);
427                 }
428
429                 oa = obdo_alloc();
430                 if (oa == NULL)
431                         GOTO(out, -ENOMEM);
432
433                 oa->o_id = lsm->lsm_object_id;
434                 oa->o_valid = OBD_MD_FLID;
435                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE);
436
437                 err = obd_destroy(ll_i2obdconn(inode), oa, lsm, NULL);
438                 obdo_free(oa);
439                 if (err)
440                         CDEBUG(D_INODE,
441                                "inode %lu obd_destroy objid "LPX64" error %d\n",
442                                inode->i_ino, lsm->lsm_object_id, err);
443         }
444 out:
445         clear_inode(inode);
446         EXIT;
447 }
448 #endif
449
450 /* like inode_setattr, but doesn't mark the inode dirty */
451 int ll_attr2inode(struct inode *inode, struct iattr *attr, int trunc)
452 {
453         unsigned int ia_valid = attr->ia_valid;
454         int error = 0;
455
456         if ((ia_valid & ATTR_SIZE) && trunc) {
457                 if (attr->ia_size > ll_file_maxbytes(inode)) {
458                         error = -EFBIG;
459                         goto out;
460                 }
461                 error = vmtruncate(inode, attr->ia_size);
462                 if (error)
463                         goto out;
464         } else if (ia_valid & ATTR_SIZE)
465                 inode->i_size = attr->ia_size;
466
467         if (ia_valid & ATTR_UID)
468                 inode->i_uid = attr->ia_uid;
469         if (ia_valid & ATTR_GID)
470                 inode->i_gid = attr->ia_gid;
471         if (ia_valid & ATTR_ATIME)
472                 inode->i_atime = attr->ia_atime;
473         if (ia_valid & ATTR_MTIME)
474                 inode->i_mtime = attr->ia_mtime;
475         if (ia_valid & ATTR_CTIME)
476                 inode->i_ctime = attr->ia_ctime;
477         if (ia_valid & ATTR_MODE) {
478                 inode->i_mode = attr->ia_mode;
479                 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
480                         inode->i_mode &= ~S_ISGID;
481         }
482 out:
483         return error;
484 }
485
486 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc)
487 {
488         struct ptlrpc_request *request = NULL;
489         struct ll_sb_info *sbi = ll_i2sbi(inode);
490         int err = 0;
491         ENTRY;
492
493         /* change incore inode */
494         err = ll_attr2inode(inode, attr, do_trunc);
495         if (err)
496                 RETURN(err);
497
498         /* Don't send size changes to MDS to avoid "fast EA" problems, and
499          * also avoid a pointless RPC (we get file size from OST anyways).
500          */
501         attr->ia_valid &= ~ATTR_SIZE;
502         if (attr->ia_valid) {
503                 struct mdc_op_data op_data;
504
505                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
506                 err = mdc_setattr(&sbi->ll_mdc_conn, &op_data,
507                                   attr, NULL, 0, NULL, 0, &request);
508                 if (err)
509                         CERROR("mdc_setattr fails: err = %d\n", err);
510
511                 ptlrpc_req_finished(request);
512                 if (S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_MTIME_SET) {
513                         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
514                         struct obdo oa;
515                         int err2;
516
517 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
518                         CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
519                                inode->i_ino, attr->ia_mtime);
520                         oa.o_mtime = attr->ia_mtime;
521 #else
522                         CDEBUG(D_INODE, "set mtime on OST inode %lu to "
523                                LPU64"\n", inode->i_ino, 
524                                ll_ts2u64(&attr->ia_mtime));
525                         oa.o_mtime = ll_ts2u64(&attr->ia_mtime);
526 #endif
527                         oa.o_id = lsm->lsm_object_id;
528                         oa.o_mode = S_IFREG;
529                         oa.o_valid = OBD_MD_FLID |OBD_MD_FLTYPE |OBD_MD_FLMTIME;
530                         err2 = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
531                         if (err2) {
532                                 CERROR("obd_setattr fails: rc=%d\n", err);
533                                 if (!err)
534                                         err = err2;
535                         }
536                 }
537         }
538
539         RETURN(err);
540 }
541
542 /* If this inode has objects allocated to it (lsm != NULL), then the OST
543  * object(s) determine the file size and mtime.  Otherwise, the MDS will
544  * keep these values until such a time that objects are allocated for it.
545  * We do the MDS operations first, as it is checking permissions for us.
546  * We don't to the MDS RPC if there is nothing that we want to store there,
547  * otherwise there is no harm in updating mtime/atime on the MDS if we are
548  * going to do an RPC anyways.
549  *
550  * If we are doing a truncate, we will send the mtime and ctime updates
551  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
552  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
553  * at the same time.
554  */
555 #define OST_ATTR (ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME | \
556                   ATTR_ATIME | ATTR_ATIME_SET | ATTR_SIZE)
557 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
558 {
559         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
560         struct ll_sb_info *sbi = ll_i2sbi(inode);
561         struct ptlrpc_request *request = NULL;
562         struct mdc_op_data op_data;
563         time_t now = LTIME_S(CURRENT_TIME);
564         int ia_valid = attr->ia_valid;
565         int rc = 0;
566         ENTRY;
567
568         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
569
570 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
571         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
572 #endif
573
574         if (ia_valid & ATTR_SIZE) {
575                 if (attr->ia_size > ll_file_maxbytes(inode)) {
576                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
577                                attr->ia_size, ll_file_maxbytes(inode));
578                         RETURN(-EFBIG);
579                 }
580
581                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
582         }
583
584         /* We mark all of the fields "set" so MDS/OST does not re-set them */
585         if (attr->ia_valid & ATTR_CTIME) {
586                 attr->ia_ctime = now;
587                 attr->ia_valid |= ATTR_CTIME_SET;
588         }
589         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
590                 attr->ia_atime = now;
591                 attr->ia_valid |= ATTR_ATIME_SET;
592         }
593         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
594                 attr->ia_mtime = now;
595                 attr->ia_valid |= ATTR_MTIME_SET;
596         }
597
598         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
599                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
600                        attr->ia_mtime, attr->ia_ctime, now);
601         if (lsm)
602                 attr->ia_valid &= ~ATTR_SIZE;
603
604         /* If only OST attributes being set on objects, don't do MDS RPC.
605          * In that case, we need to check permissions and update the local
606          * inode ourselves so we can call obdo_from_inode() always. */
607         if (ia_valid & (lsm ? ~(OST_ATTR | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
608                 struct lustre_md md;
609                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
610
611                 rc = mdc_setattr(&sbi->ll_mdc_conn, &op_data,
612                                   attr, NULL, 0, NULL, 0, &request);
613
614                 if (rc) {
615                         ptlrpc_req_finished(request);
616                         if (rc != -EPERM && rc != -EACCES)
617                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
618                         RETURN(rc);
619                 }
620
621                 rc = mdc_req2lustre_md(request, 0, &sbi->ll_osc_conn, &md);
622                 if (rc) {
623                         ptlrpc_req_finished(request);
624                         RETURN(rc);
625                 }
626                 ll_update_inode(inode, md.body, md.lsm);
627                 ptlrpc_req_finished(request);
628
629                 if (!md.lsm || !S_ISREG(inode->i_mode)) {
630                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
631                         RETURN(0);
632                 }
633         } else {
634                 /* The OST doesn't check permissions, but the alternative is
635                  * a gratuitous RPC to the MDS.  We already rely on the client
636                  * to do read/write/truncate permission checks, so is mtime OK?
637                  */
638                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
639                         /* from sys_utime() */
640                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
641                                 if (current->fsuid != inode->i_uid &&
642                                     (rc = permission(inode, MAY_WRITE)) != 0)
643                                         RETURN(rc);
644                         } else {
645                                 /* from inode_change_ok() */
646                                 if (current->fsuid != inode->i_uid &&
647                                     !capable(CAP_FOWNER))
648                                         RETURN(-EPERM);
649                         }
650                 }
651
652                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
653                 inode_setattr(inode, attr);
654         }
655
656         if (ia_valid & ATTR_SIZE) {
657                 struct ldlm_extent extent = { .start = attr->ia_size,
658                                               .end = OBD_OBJECT_EOF };
659                 struct lustre_handle lockh = { 0 };
660                 int err;
661
662                 /* Writeback uses inode->i_size to determine how far out
663                  * its cached pages go.  ll_truncate gets a PW lock, canceling
664                  * our lock, _after_ it has updated i_size.  this can confuse
665                  *
666                  * We really need to get our PW lock before we change
667                  * inode->i_size.  If we don't we can race with other
668                  * i_size updaters on our node, like ll_file_read.  We
669                  * can also race with i_size propogation to other
670                  * nodes through dirtying and writeback of final cached
671                  * pages.  This last one is especially bad for racing
672                  * o_append users on other nodes. */
673                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
674                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
675                 up(&inode->i_sem);
676                 rc = ll_extent_lock_no_validate(NULL, inode, lsm, LCK_PW,
677                                                  &extent, &lockh);
678                 down(&inode->i_sem);
679                 if (rc != ELDLM_OK) {
680                         if (rc > 0)
681                                 RETURN(-ENOLCK);
682                         RETURN(rc);
683                 }
684
685                 rc = vmtruncate(inode, attr->ia_size);
686                 if (rc == 0)
687                         set_bit(LLI_F_HAVE_SIZE_LOCK,
688                                 &ll_i2info(inode)->lli_flags);
689
690                 /* unlock now as we don't mind others file lockers racing with
691                  * the mds updates below? */
692                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
693                 if (err) {
694                         CERROR("ll_extent_unlock failed: %d\n", err);
695                         if (!rc)
696                                 rc = err;
697                 }
698         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
699                 struct obdo oa;
700
701                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
702                        inode->i_ino, attr->ia_mtime);
703                 oa.o_id = lsm->lsm_object_id;
704                 oa.o_valid = OBD_MD_FLID;
705                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
706                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
707                 rc = obd_setattr(&sbi->ll_osc_conn, &oa, lsm, NULL);
708                 if (rc)
709                         CERROR("obd_setattr fails: rc=%d\n", rc);
710         }
711         RETURN(rc);
712 }
713
714 int ll_setattr(struct dentry *de, struct iattr *attr)
715 {
716         int rc = inode_change_ok(de->d_inode, attr);
717         CDEBUG(D_VFSTRACE, "VFS Op:name=%s\n", de->d_name.name);
718         if (rc)
719                 return rc;
720
721         lprocfs_counter_incr(ll_i2sbi(de->d_inode)->ll_stats, LPROC_LL_SETATTR);
722         return ll_inode_setattr(de->d_inode, attr, 1);
723 }
724
725 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
726                        unsigned long max_age)
727 {
728         struct ll_sb_info *sbi = ll_s2sbi(sb);
729         struct obd_statfs obd_osfs;
730         int rc;
731         ENTRY;
732
733         rc = obd_statfs(class_conn2obd(&sbi->ll_mdc_conn), osfs, max_age);
734         if (rc) {
735                 CERROR("mdc_statfs fails: rc = %d\n", rc);
736                 RETURN(rc);
737         }
738
739         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
740                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
741
742         rc = obd_statfs(class_conn2obd(&sbi->ll_osc_conn), &obd_osfs, max_age);
743         if (rc) {
744                 CERROR("obd_statfs fails: rc = %d\n", rc);
745                 RETURN(rc);
746         }
747
748         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
749                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
750                obd_osfs.os_files);
751
752         osfs->os_blocks = obd_osfs.os_blocks;
753         osfs->os_bfree = obd_osfs.os_bfree;
754         osfs->os_bavail = obd_osfs.os_bavail;
755
756         /* If we don't have as many objects free on the OST as inodes
757          * on the MDS, we reduce the total number of inodes to
758          * compensate, so that the "inodes in use" number is correct.
759          */
760         if (obd_osfs.os_ffree < osfs->os_ffree) {
761                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
762                         obd_osfs.os_ffree;
763                 osfs->os_ffree = obd_osfs.os_ffree;
764         }
765
766         RETURN(rc);
767 }
768
769 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
770 {
771         struct obd_statfs osfs;
772         int rc;
773
774         CDEBUG(D_VFSTRACE, "VFS Op:\n");
775         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
776
777         /* For now we will always get up-to-date statfs values, but in the
778          * future we may allow some amount of caching on the client (e.g.
779          * from QOS or lprocfs updates). */
780         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
781         if (rc)
782                 return rc;
783
784         statfs_unpack(sfs, &osfs);
785
786         if (sizeof(sfs->f_blocks) == 4) {
787                 while (osfs.os_blocks > ~0UL) {
788                         sfs->f_bsize <<= 1;
789
790                         osfs.os_blocks >>= 1;
791                         osfs.os_bfree >>= 1;
792                         osfs.os_bavail >>= 1;
793                 }
794         }
795
796         sfs->f_blocks = osfs.os_blocks;
797         sfs->f_bfree = osfs.os_bfree;
798         sfs->f_bavail = osfs.os_bavail;
799
800         return 0;
801 }
802
803 void dump_lsm(int level, struct lov_stripe_md *lsm)
804 {
805         CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic %#08x, "
806                "stripe_size %#08x, offset %u, stripe_count %u\n",
807                lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
808                lsm->lsm_stripe_size, lsm->lsm_stripe_offset,
809                lsm->lsm_stripe_count);
810 }
811
812 void ll_update_inode(struct inode *inode, struct mds_body *body,
813                      struct lov_stripe_md *lsm)
814 {
815         struct ll_inode_info *lli = ll_i2info(inode);
816
817         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
818         if (lsm != NULL) {
819                 if (lli->lli_smd == NULL) {
820                         lli->lli_smd = lsm;
821                         lli->lli_maxbytes = lsm->lsm_maxbytes;
822                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
823                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
824                 } else {
825                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
826                                 CERROR("lsm mismatch for inode %ld\n",
827                                        inode->i_ino);
828                                 CERROR("lli_smd:\n");
829                                 dump_lsm(D_ERROR, lli->lli_smd);
830                                 CERROR("lsm:\n");
831                                 dump_lsm(D_ERROR, lsm);
832                                 LBUG();
833                         }
834                 }
835         }
836
837         if (body->valid & OBD_MD_FLID)
838                 inode->i_ino = body->ino;
839         if (body->valid & OBD_MD_FLATIME)
840                 LTIME_S(inode->i_atime) = body->atime;
841         if (body->valid & OBD_MD_FLMTIME)
842                 LTIME_S(inode->i_mtime) = body->mtime;
843         if (body->valid & OBD_MD_FLCTIME)
844                 LTIME_S(inode->i_ctime) = body->ctime;
845         if (body->valid & OBD_MD_FLMODE)
846                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
847         if (body->valid & OBD_MD_FLTYPE)
848                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
849         if (body->valid & OBD_MD_FLUID)
850                 inode->i_uid = body->uid;
851         if (body->valid & OBD_MD_FLGID)
852                 inode->i_gid = body->gid;
853         if (body->valid & OBD_MD_FLFLAGS)
854                 inode->i_flags = body->flags;
855         if (body->valid & OBD_MD_FLNLINK)
856                 inode->i_nlink = body->nlink;
857         if (body->valid & OBD_MD_FLGENER)
858                 inode->i_generation = body->generation;
859         if (body->valid & OBD_MD_FLRDEV)
860 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
861                 inode->i_rdev = body->rdev;
862 #else
863                 inode->i_rdev = to_kdev_t(body->rdev);
864 #endif
865         if (body->valid & OBD_MD_FLSIZE)
866                 inode->i_size = body->size;
867         if (body->valid & OBD_MD_FLBLOCKS)
868                 inode->i_blocks = body->blocks;
869 }
870
871 void ll_read_inode2(struct inode *inode, void *opaque)
872 {
873         struct lustre_md *md = opaque;
874         struct ll_inode_info *lli = ll_i2info(inode);
875         ENTRY;
876
877         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
878                inode->i_generation, inode);
879
880         ll_lli_init(lli);
881
882         LASSERT(!lli->lli_smd);
883
884         /* core attributes from the MDS first */
885         ll_update_inode(inode, md->body, md->lsm);
886
887         /* OIDEBUG(inode); */
888
889         if (S_ISREG(inode->i_mode)) {
890                 inode->i_op = &ll_file_inode_operations;
891                 inode->i_fop = &ll_file_operations;
892                 inode->i_mapping->a_ops = &ll_aops;
893                 EXIT;
894         } else if (S_ISDIR(inode->i_mode)) {
895                 inode->i_op = &ll_dir_inode_operations;
896                 inode->i_fop = &ll_dir_operations;
897                 inode->i_mapping->a_ops = &ll_dir_aops;
898                 EXIT;
899         } else if (S_ISLNK(inode->i_mode)) {
900                 inode->i_op = &ll_fast_symlink_inode_operations;
901                 EXIT;
902         } else {
903                 inode->i_op = &ll_special_inode_operations;
904 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
905                 init_special_inode(inode, inode->i_mode, 
906                                    kdev_t_to_nr(inode->i_rdev));
907 #else
908                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
909 #endif
910                 EXIT;
911         }
912 }
913
914 int it_disposition(struct lookup_intent *it, int flag)
915 {
916         return it->it_disposition & flag;
917 }
918
919 void it_set_disposition(struct lookup_intent *it, int flag)
920 {
921         it->it_disposition |= flag;
922 }
923
924 void ll_umount_begin(struct super_block *sb)
925 {
926         struct ll_sb_info *sbi = ll_s2sbi(sb);
927         struct obd_device *obd;
928         struct obd_ioctl_data ioc_data = { 0 };
929         ENTRY;
930         CDEBUG(D_VFSTRACE, "VFS Op:\n");
931
932         obd = class_conn2obd(&sbi->ll_mdc_conn);
933         if (obd == NULL) {
934                 CERROR("Invalid MDC connection handle "LPX64"\n",
935                        sbi->ll_mdc_conn.cookie);
936                 EXIT;
937                 return;
938         }
939         obd->obd_no_recov = 1;
940         obd_iocontrol(IOC_OSC_SET_ACTIVE, &sbi->ll_mdc_conn, sizeof ioc_data,
941                       &ioc_data, NULL);
942
943         obd = class_conn2obd(&sbi->ll_osc_conn);
944         obd->obd_no_recov = 1;
945         obd_iocontrol(IOC_OSC_SET_ACTIVE, &sbi->ll_osc_conn, sizeof ioc_data,
946                       &ioc_data, NULL);
947
948         /* Really, we'd like to wait until there are no requests outstanding,
949          * and then continue.  For now, we just invalidate the requests,
950          * schedule, and hope.
951          */
952         schedule();
953
954         EXIT;
955 }