Whamcloud - gitweb
- fixes about removing lmv_obj when coresponding inode is getting removed. Former
[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/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_dlm.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
42 #ifndef log2
43 #define log2(n) ffz(~(n))
44 #endif
45
46 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
47 {
48         struct ll_sb_info *sbi = NULL;
49         class_uuid_t uuid;
50         ENTRY;
51
52         OBD_ALLOC(sbi, sizeof(*sbi));
53         if (!sbi)
54                 RETURN(NULL);
55
56         spin_lock_init(&sbi->ll_lock);
57         INIT_LIST_HEAD(&sbi->ll_pglist);
58         sbi->ll_pglist_gen = 0;
59         if (num_physpages < SBI_DEFAULT_RA_MAX / 4)
60                 sbi->ll_ra_info.ra_max_pages = num_physpages / 4;
61         else
62                 sbi->ll_ra_info.ra_max_pages = SBI_DEFAULT_RA_MAX;
63         INIT_LIST_HEAD(&sbi->ll_conn_chain);
64         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
65         INIT_LIST_HEAD(&sbi->ll_mnt_list);
66         sema_init(&sbi->ll_gns_sem, 1);
67         init_completion(&sbi->ll_gns_completion);
68         sbi->ll_gns_state = LL_GNS_STATE_IDLE;
69         sbi->ll_gns_timer.data = (unsigned long)sbi;
70         sbi->ll_gns_timer.function = ll_gns_timer_callback;
71         init_timer(&sbi->ll_gns_timer);
72         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
73
74         ll_set_sbi(sb, sbi);
75
76         generate_random_uuid(uuid);
77         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
78         RETURN(sbi);
79 }
80
81 void lustre_free_sbi(struct super_block *sb)
82 {
83         struct ll_sb_info *sbi = ll_s2sbi(sb);
84         ENTRY;
85
86         if (sbi != NULL) {
87                 list_del(&sbi->ll_gns_sbi_head);
88                 del_timer(&sbi->ll_gns_timer);
89                 OBD_FREE(sbi, sizeof(*sbi));
90         }
91         ll_set_sbi(sb, NULL);
92         EXIT;
93 }
94
95 int lustre_init_lov_desc(struct ll_sb_info *sbi)
96 {
97         int valsize, rc;
98         ENTRY;
99         
100         valsize = sizeof(sbi->ll_lov_desc);
101         memset(&sbi->ll_lov_desc, 0, sizeof(sbi->ll_lov_desc));
102         rc = obd_get_info(sbi->ll_lov_exp, strlen("lovdesc") + 1,
103                           "lovdesc", &valsize, &sbi->ll_lov_desc);
104         RETURN(rc);
105 }
106
107 int lustre_init_lmv_desc(struct ll_sb_info *sbi)
108 {
109         int valsize, rc;
110         ENTRY;
111         
112         valsize = sizeof(sbi->ll_lmv_desc);
113         memset(&sbi->ll_lmv_desc, 0, sizeof(sbi->ll_lmv_desc));
114         rc = obd_get_info(sbi->ll_lmv_exp, strlen("lmvdesc") + 1,
115                           "lmvdesc", &valsize, &sbi->ll_lmv_desc);
116         RETURN(rc);
117 }
118
119 int lustre_common_fill_super(struct super_block *sb, char *lmv, char *lov)
120 {
121         struct ll_sb_info *sbi = ll_s2sbi(sb);
122         struct ptlrpc_request *request = NULL;
123         struct lustre_handle lov_conn = {0, };
124         struct lustre_handle lmv_conn = {0, };
125         struct inode *root = NULL;
126         struct obd_device *obd;
127         struct obd_statfs osfs;
128         struct lustre_md md;
129         kdev_t devno;
130         int err;
131         ENTRY;
132
133         obd = class_name2obd(lmv);
134         if (!obd) {
135                 CERROR("MDC %s: not setup or attached\n", lmv);
136                 RETURN(-EINVAL);
137         }
138
139         if (proc_lustre_fs_root) {
140                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
141                                                   lov, lmv);
142                 if (err < 0)
143                         CERROR("could not register mount in /proc/lustre");
144         }
145
146         err = obd_connect(&lmv_conn, obd, &sbi->ll_sb_uuid, OBD_OPT_REAL_CLIENT);
147         if (err == -EBUSY) {
148                 CERROR("An MDS (mdc %s) is performing recovery, of which this"
149                        " client is not a part.  Please wait for recovery to "
150                        "complete, abort, or time out.\n", lmv);
151                 GOTO(out, err);
152         } else if (err) {
153                 CERROR("cannot connect to %s: rc = %d\n", lmv, err);
154                 GOTO(out, err);
155         }
156         sbi->ll_lmv_exp = class_conn2export(&lmv_conn);
157
158         lustre_init_lmv_desc(sbi);
159         
160         err = obd_statfs(obd, &osfs, jiffies - HZ);
161         if (err)
162                 GOTO(out_lmv, err);
163
164         if (!osfs.os_bsize) {
165                 CERROR("Invalid block size is detected.");
166                 GOTO(out_lmv, err);
167         }
168
169         sb->s_magic = LL_SUPER_MAGIC;
170         sb->s_blocksize = osfs.os_bsize;
171         sb->s_blocksize_bits = log2(osfs.os_bsize);
172         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
173        
174         devno = get_uuid2int(sbi->ll_lmv_exp->exp_obd->obd_uuid.uuid, 
175                              strlen(sbi->ll_lmv_exp->exp_obd->obd_uuid.uuid));
176
177         sb->s_dev = devno;
178
179         obd = class_name2obd(lov);
180         if (!obd) {
181                 CERROR("OSC %s: not setup or attached\n", lov);
182                 GOTO(out_lmv, err);
183         }
184
185         err = obd_connect(&lov_conn, obd, &sbi->ll_sb_uuid, OBD_OPT_REAL_CLIENT);
186         if (err == -EBUSY) {
187                 CERROR("An OST (osc %s) is performing recovery, of which this"
188                        " client is not a part.  Please wait for recovery to "
189                        "complete, abort, or time out.\n", lov);
190                 GOTO(out, err);
191         } else if (err) {
192                 CERROR("cannot connect to %s: rc = %d\n", lov, err);
193                 GOTO(out_lmv, err);
194         }
195         sbi->ll_lov_exp = class_conn2export(&lov_conn);
196
197         err = lustre_init_lov_desc(sbi);
198         if (err == 0) {
199                 int mdsize = obd_size_diskmd(sbi->ll_lov_exp, NULL);
200                 obd_init_ea_size(sbi->ll_lmv_exp, mdsize,
201                                  sbi->ll_lov_desc.ld_tgt_count *
202                                  sizeof(struct llog_cookie));
203         }
204         
205         err = md_getstatus(sbi->ll_lmv_exp, &sbi->ll_rootid);
206         if (err) {
207                 CERROR("cannot mds_connect: rc = %d\n", err);
208                 GOTO(out_lov, err);
209         }
210         CDEBUG(D_SUPER, "rootid "DLID4"\n", OLID4(&sbi->ll_rootid));
211
212         sb->s_op = &lustre_super_operations;
213
214         /* make root inode
215          * XXX: move this to after cbd setup? */
216         err = md_getattr(sbi->ll_lmv_exp, &sbi->ll_rootid,
217                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS | OBD_MD_FID,
218                          0, &request);
219         if (err) {
220                 CERROR("md_getattr failed for root: rc = %d\n", err);
221                 GOTO(out_lov, err);
222         }
223
224         err = mdc_req2lustre_md(sbi->ll_lmv_exp, request, 0, 
225                                 sbi->ll_lov_exp, &md);
226         if (err) {
227                 CERROR("failed to understand root inode md: rc = %d\n", err);
228                 ptlrpc_req_finished(request);
229                 GOTO(out_lov, err);
230         }
231
232         LASSERT(id_ino(&sbi->ll_rootid) != 0);
233         root = ll_iget(sb, id_ino(&sbi->ll_rootid), &md);
234
235         ptlrpc_req_finished(request);
236
237         if (root == NULL || is_bad_inode(root)) {
238                 CERROR("lustre_lite: bad iget4 for root\n");
239                 GOTO(out_root, err = -EBADF);
240         }
241
242         err = ll_close_thread_start(&sbi->ll_lcq);
243         if (err) {
244                 CERROR("cannot start close thread: rc %d\n", err);
245                 GOTO(out_root, err);
246         }
247
248         ll_gns_add_timer(sbi);
249
250         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x, backing dev
251            info assigned to inode mapping is used for determining maximal
252            readahead. */
253 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
254         /* bug 2805 - set VM readahead to zero */
255         vm_max_readahead = vm_min_readahead = 0;
256 #endif
257
258         sb->s_root = d_alloc_root(root);
259
260 #ifdef S_PDIROPS
261         CWARN("Enabling PDIROPS\n");
262         sb->s_flags |= S_PDIROPS;
263 #endif
264
265         RETURN(err);
266 out_root:
267         if (root)
268                 iput(root);
269 out_lov:
270         obd_disconnect(sbi->ll_lov_exp, 0);
271 out_lmv:
272         obd_disconnect(sbi->ll_lmv_exp, 0);
273 out:
274         lprocfs_unregister_mountpoint(sbi);
275         return err;
276 }
277
278 void lustre_common_put_super(struct super_block *sb)
279 {
280         struct ll_sb_info *sbi = ll_s2sbi(sb);
281         struct hlist_node *tmp, *next;
282         ENTRY;
283
284         ll_gns_del_timer(sbi);
285         ll_close_thread_shutdown(sbi->ll_lcq);
286
287         list_del(&sbi->ll_conn_chain);
288         obd_disconnect(sbi->ll_lov_exp, 0);
289
290         lprocfs_unregister_mountpoint(sbi);
291         if (sbi->ll_proc_root) {
292                 lprocfs_remove(sbi->ll_proc_root);
293                 sbi->ll_proc_root = NULL;
294         }
295
296         obd_disconnect(sbi->ll_lmv_exp, 0);
297
298         // We do this to get rid of orphaned dentries. That is not really trw.
299         spin_lock(&dcache_lock);
300         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
301                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
302                 CWARN("orphan dentry %*s (%p) at unmount\n",
303                       dentry->d_name.len, dentry->d_name.name, dentry);
304                 shrink_dcache_parent(dentry);
305         }
306         spin_unlock(&dcache_lock);
307         EXIT;
308 }
309
310 char *ll_read_opt(const char *opt, char *data)
311 {
312         char *value;
313         char *retval;
314         ENTRY;
315
316         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
317         if (strncmp(opt, data, strlen(opt)))
318                 RETURN(NULL);
319         if ((value = strchr(data, '=')) == NULL)
320                 RETURN(NULL);
321
322         value++;
323         OBD_ALLOC(retval, strlen(value) + 1);
324         if (!retval) {
325                 CERROR("out of memory!\n");
326                 RETURN(NULL);
327         }
328
329         memcpy(retval, value, strlen(value)+1);
330         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
331         RETURN(retval);
332 }
333
334 int ll_set_opt(const char *opt, char *data, int fl)
335 {
336         ENTRY;
337
338         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
339         if (strncmp(opt, data, strlen(opt)))
340                 RETURN(0);
341         else
342                 RETURN(fl);
343 }
344
345 void ll_options(char *options, char **lov, char **lmv, int *flags)
346 {
347         char *this_char;
348 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
349         char *opt_ptr = options;
350 #endif
351         ENTRY;
352
353         if (!options) {
354                 EXIT;
355                 return;
356         }
357
358 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
359         for (this_char = strtok (options, ",");
360              this_char != NULL;
361              this_char = strtok (NULL, ",")) {
362 #else
363         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
364 #endif
365                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
366                 if (!*lov && (*lov = ll_read_opt("osc", this_char)))
367                         continue;
368                 if (!*lmv && (*lmv = ll_read_opt("mdc", this_char)))
369                         continue;
370                 if (!(*flags & LL_SBI_NOLCK) &&
371                     ((*flags) = (*flags) |
372                                 ll_set_opt("nolock", this_char,
373                                            LL_SBI_NOLCK)))
374                         continue;
375         }
376         
377         EXIT;
378 }
379
380 void ll_lli_init(struct ll_inode_info *lli)
381 {
382         sema_init(&lli->lli_open_sem, 1);
383         lli->lli_flags = 0;
384         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
385         spin_lock_init(&lli->lli_lock);
386         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
387         lli->lli_inode_magic = LLI_INODE_MAGIC;
388         memset(&lli->lli_id, 0, sizeof(lli->lli_id));
389 }
390
391 int ll_fill_super(struct super_block *sb, void *data, int silent)
392 {
393         struct ll_sb_info *sbi;
394         char *lov = NULL;
395         char *lmv = NULL;
396         int err;
397         ENTRY;
398
399         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
400
401         sbi = lustre_init_sbi(sb);
402         if (!sbi)
403                 RETURN(-ENOMEM);
404
405         sbi->ll_flags |= LL_SBI_READAHEAD;
406         ll_options(data, &lov, &lmv, &sbi->ll_flags);
407
408         if (!lov) {
409                 CERROR("no osc\n");
410                 GOTO(out, err = -EINVAL);
411         }
412
413         if (!lmv) {
414                 CERROR("no mdc\n");
415                 GOTO(out, err = -EINVAL);
416         }
417
418         err = lustre_common_fill_super(sb, lmv, lov);
419         EXIT;
420 out:
421         if (err)
422                 lustre_free_sbi(sb);
423
424         if (lmv)
425                 OBD_FREE(lmv, strlen(lmv) + 1);
426         if (lov)
427                 OBD_FREE(lov, strlen(lov) + 1);
428         return err;
429 } /* ll_read_super */
430
431 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
432                               struct config_llog_instance *cfg, int allow_recov)
433 {
434         struct lustre_cfg lcfg;
435         struct portals_cfg pcfg;
436         char *peer = "MDS_PEER_UUID";
437         struct obd_device *obd;
438         struct lustre_handle lmv_conn = {0, };
439         struct obd_export *exp;
440         char *name = "mdc_dev";
441         class_uuid_t uuid;
442         struct obd_uuid lmv_uuid;
443         struct llog_ctxt *ctxt;
444         int rc = 0;
445         int err;
446         ENTRY;
447
448         if (lmd_bad_magic(lmd))
449                 RETURN(-EINVAL);
450
451         generate_random_uuid(uuid);
452         class_uuid_unparse(uuid, &lmv_uuid);
453
454         if (lmd->lmd_local_nid) {
455                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
456                 pcfg.pcfg_nal = lmd->lmd_nal;
457                 pcfg.pcfg_nid = lmd->lmd_local_nid;
458                 err = libcfs_nal_cmd(&pcfg);
459                 if (err <0)
460                         GOTO(out, err);
461         }
462
463         if (lmd->lmd_nal == SOCKNAL ||
464               lmd->lmd_nal == OPENIBNAL) {
465                 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
466                 pcfg.pcfg_nal     = lmd->lmd_nal;
467                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
468                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
469                 pcfg.pcfg_misc    = lmd->lmd_port;
470                 pcfg.pcfg_size    = 8388608;
471                 pcfg.pcfg_flags   = 0x4; /*share*/
472                 err = libcfs_nal_cmd(&pcfg);
473                 if (err <0)
474                         GOTO(out, err);
475         }
476
477         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
478         lcfg.lcfg_nid = lmd->lmd_server_nid;
479         lcfg.lcfg_inllen1 = strlen(peer) + 1;
480         lcfg.lcfg_inlbuf1 = peer;
481         lcfg.lcfg_nal = lmd->lmd_nal;
482         err = class_process_config(&lcfg);
483         if (err < 0)
484                 GOTO(out_del_conn, err);
485
486         LCFG_INIT(lcfg, LCFG_ATTACH, name);
487         lcfg.lcfg_inlbuf1 = "mdc";
488         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
489         lcfg.lcfg_inlbuf2 = lmv_uuid.uuid;
490         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
491         err = class_process_config(&lcfg);
492         if (err < 0)
493                 GOTO(out_del_uuid, err);
494
495         LCFG_INIT(lcfg, LCFG_SETUP, name);
496         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
497         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
498         lcfg.lcfg_inlbuf2 = peer;
499         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
500         err = class_process_config(&lcfg);
501         if (err < 0)
502                 GOTO(out_detach, err);
503
504         obd = class_name2obd(name);
505         if (obd == NULL)
506                 GOTO(out_cleanup, err = -EINVAL);
507
508         /* Disable initial recovery on this import */
509         err = obd_set_info(obd->obd_self_export,
510                            strlen("initial_recov"), "initial_recov",
511                            sizeof(allow_recov), &allow_recov);
512         if (err)
513                 GOTO(out_cleanup, err);
514
515         err = obd_connect(&lmv_conn, obd, &lmv_uuid, 0);
516         if (err) {
517                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
518                 GOTO(out_cleanup, err);
519         }
520
521         exp = class_conn2export(&lmv_conn);
522
523         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
524         rc = class_config_process_llog(ctxt, profile, cfg);
525         if (rc)
526                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
527
528         err = obd_disconnect(exp, 0);
529         
530         EXIT;
531 out_cleanup:
532         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
533         err = class_process_config(&lcfg);
534         if (err < 0)
535                 GOTO(out, err);
536
537 out_detach:
538         LCFG_INIT(lcfg, LCFG_DETACH, name);
539         err = class_process_config(&lcfg);
540         if (err < 0)
541                 GOTO(out, err);
542
543 out_del_uuid:
544         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
545         lcfg.lcfg_inllen1 = strlen(peer) + 1;
546         lcfg.lcfg_inlbuf1 = peer;
547         err = class_process_config(&lcfg);
548
549 out_del_conn:
550         if (lmd->lmd_nal == SOCKNAL ||
551             lmd->lmd_nal == OPENIBNAL) {
552                 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
553                 pcfg.pcfg_nal     = lmd->lmd_nal;
554                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
555                 pcfg.pcfg_flags   = 1;          /* single_share */
556                 err = libcfs_nal_cmd(&pcfg);
557                 if (err <0)
558                         GOTO(out, err);
559         }
560 out:
561         if (rc == 0)
562                 rc = err;
563
564         return rc;
565 }
566
567 int lustre_fill_super(struct super_block *sb, void *data, int silent)
568 {
569         struct lustre_mount_data * lmd = data;
570         char *lov = NULL, *lmv = NULL;
571         struct ll_sb_info *sbi;
572         int err;
573         ENTRY;
574
575         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
576         if (lmd_bad_magic(lmd))
577                 RETURN(-EINVAL);
578
579         sbi = lustre_init_sbi(sb);
580         if (!sbi)
581                 RETURN(-ENOMEM);
582
583         sbi->ll_flags |= LL_SBI_READAHEAD;
584
585         if (lmd->lmd_profile) {
586                 struct lustre_profile *lprof;
587                 struct config_llog_instance cfg;
588                 int len;
589
590                 if (lmd->lmd_mds[0] == '\0') {
591                         CERROR("no mds name\n");
592                         GOTO(out_free, err = -EINVAL);
593                 }
594
595                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
596                 if (sbi->ll_lmd == NULL)
597                         GOTO(out_free, err = -ENOMEM);
598                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
599
600                 /* generate a string unique to this super, let's try
601                  the address of the super itself.*/
602                 len = (sizeof(sb) * 2) + 1;
603                 OBD_ALLOC(sbi->ll_instance, len);
604                 if (sbi->ll_instance == NULL)
605                         GOTO(out_free, err = -ENOMEM);
606                 sprintf(sbi->ll_instance, "%p", sb);
607
608                 cfg.cfg_instance = sbi->ll_instance;
609                 cfg.cfg_uuid = sbi->ll_sb_uuid;
610                 cfg.cfg_local_nid = lmd->lmd_local_nid;
611                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
612                 if (err < 0) {
613                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
614                         GOTO(out_free, err);
615                 }
616
617                 lprof = class_get_profile(lmd->lmd_profile);
618                 if (lprof == NULL) {
619                         CERROR("No profile found: %s\n", lmd->lmd_profile);
620                         GOTO(out_free, err = -EINVAL);
621                 }
622                 if (lov)
623                         OBD_FREE(lov, strlen(lov) + 1);
624                 OBD_ALLOC(lov, strlen(lprof->lp_lov) +
625                           strlen(sbi->ll_instance) + 2);
626                 sprintf(lov, "%s-%s", lprof->lp_lov, sbi->ll_instance);
627
628                 if (lmv)
629                         OBD_FREE(lmv, strlen(lmv) + 1);
630                 OBD_ALLOC(lmv, strlen(lprof->lp_lmv) +
631                           strlen(sbi->ll_instance) + 2);
632                 sprintf(lmv, "%s-%s", lprof->lp_lmv, sbi->ll_instance);
633         }
634
635         if (!lov) {
636                 CERROR("no osc\n");
637                 GOTO(out_free, err = -EINVAL);
638         }
639
640         if (!lmv) {
641                 CERROR("no mdc\n");
642                 GOTO(out_free, err = -EINVAL);
643         }
644
645         err = lustre_common_fill_super(sb, lmv, lov);
646
647         if (err)
648                 GOTO(out_free, err);
649         
650 out_dev:
651         if (lmv)
652                 OBD_FREE(lmv, strlen(lmv) + 1);
653         if (lov)
654                 OBD_FREE(lov, strlen(lov) + 1);
655
656         RETURN(err);
657
658 out_free:
659         if (sbi->ll_lmd) {
660                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
661                 int err;
662
663                 if (sbi->ll_instance != NULL) {
664                         struct lustre_mount_data *lmd = sbi->ll_lmd;
665                         char * cln_prof;
666                         struct config_llog_instance cfg;
667
668                         cfg.cfg_instance = sbi->ll_instance;
669                         cfg.cfg_uuid = sbi->ll_sb_uuid;
670
671                         OBD_ALLOC(cln_prof, len);
672                         sprintf(cln_prof, "%s-clean", lmd->lmd_profile);
673
674                         err = lustre_process_log(lmd, cln_prof, &cfg, 0);
675                         if (err < 0)
676                                 CERROR("Unable to process log: %s\n", cln_prof);
677                         OBD_FREE(cln_prof, len);
678                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
679                 }
680                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
681         }
682         lustre_free_sbi(sb);
683
684         goto out_dev;
685 } /* lustre_fill_super */
686
687 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
688 {
689         struct lustre_cfg lcfg;
690         struct obd_device *obd;
691         int next = 0;
692
693         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
694         {
695                 int err;
696
697                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
698                 err = class_process_config(&lcfg);
699                 if (err) {
700                         CERROR("cleanup failed: %s\n", obd->obd_name);
701                         //continue;
702                 }
703
704                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
705                 err = class_process_config(&lcfg);
706                 if (err) {
707                         CERROR("detach failed: %s\n", obd->obd_name);
708                         //continue;
709                 }
710         }
711
712         if (sbi->ll_lmd != NULL)
713                 class_del_profile(sbi->ll_lmd->lmd_profile);
714 }
715
716 void lustre_put_super(struct super_block *sb)
717 {
718         struct obd_device *obd;
719         struct ll_sb_info *sbi = ll_s2sbi(sb);
720         int force_umount = 0;
721         ENTRY;
722
723         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
724         obd = class_exp2obd(sbi->ll_lmv_exp);
725         if (obd)
726                 force_umount = obd->obd_no_recov;
727         obd = NULL;
728
729         lustre_common_put_super(sb);
730         if (sbi->ll_lmd != NULL) {
731                 char * cln_prof;
732                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
733                 int err;
734                 struct config_llog_instance cfg;
735
736                 if (force_umount) {
737                         CERROR("force umount, doing manual cleanup\n");
738                         lustre_manual_cleanup(sbi);
739                         GOTO(free_lmd, 0);
740                 }
741
742                 cfg.cfg_instance = sbi->ll_instance;
743                 cfg.cfg_uuid = sbi->ll_sb_uuid;
744
745                 OBD_ALLOC(cln_prof, len);
746                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
747
748                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
749                 if (err < 0) {
750                         CERROR("Unable to process log: %s, doing manual cleanup"
751                                "\n", cln_prof);
752                         lustre_manual_cleanup(sbi);
753                 }
754
755                 OBD_FREE(cln_prof, len);
756         free_lmd:
757                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
758                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
759         }
760
761         lustre_free_sbi(sb);
762
763         EXIT;
764 } /* lustre_put_super */
765
766 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
767 {
768         struct obd_export *lmv_exp = sbi->ll_lmv_exp;
769         struct lustre_mount_data *lmd = sbi->ll_lmd;
770         struct llog_ctxt *ctxt;
771         struct config_llog_instance cfg;
772         char *profile = lmd->lmd_profile, *name = NULL;
773         int rc, namelen =  0, version;
774         ENTRY;
775
776         if (profile == NULL)
777                 RETURN(0);
778         if (lmd == NULL) {
779                 CERROR("Client not mounted with zero-conf; cannot process "
780                        "update log.\n");
781                 RETURN(0);
782         }
783
784         rc = ldlm_cli_cancel_unused(lmv_exp->exp_obd->obd_namespace, NULL,
785                                     LDLM_FL_CONFIG_CHANGE, NULL);
786         if (rc != 0)
787                 CWARN("ldlm_cli_cancel_unused(mdc): %d\n", rc);
788
789         rc = obd_cancel_unused(sbi->ll_lov_exp, NULL, LDLM_FL_CONFIG_CHANGE,
790                                NULL);
791         if (rc != 0)
792                 CWARN("obd_cancel_unused(lov): %d\n", rc);
793
794         cfg.cfg_instance = sbi->ll_instance;
795         cfg.cfg_uuid = sbi->ll_sb_uuid;
796         cfg.cfg_local_nid = lmd->lmd_local_nid;
797
798         namelen = strlen(profile) + 20; /* -clean-######### */
799         OBD_ALLOC(name, namelen);
800         if (name == NULL)
801                 RETURN(-ENOMEM);
802
803         if (clean) {
804                 version = sbi->ll_config_version - 1;
805                 sprintf(name, "%s-clean-%d", profile, version);
806         } else {
807                 version = sbi->ll_config_version + 1;
808                 sprintf(name, "%s-%d", profile, version);
809         }
810
811         CWARN("Applying configuration log %s\n", name);
812
813         ctxt = llog_get_context(&lmv_exp->exp_obd->obd_llogs,
814                                 LLOG_CONFIG_REPL_CTXT);
815         rc = class_config_process_llog(ctxt, name, &cfg);
816         if (rc == 0)
817                 sbi->ll_config_version = version;
818         CWARN("Finished applying configuration log %s: %d\n", name, rc);
819
820         if (rc == 0 && clean == 0) {
821                 struct lov_desc desc;
822                 int rc, valsize;
823                 valsize = sizeof(desc);
824                 rc = obd_get_info(sbi->ll_lov_exp, strlen("lovdesc") + 1,
825                                   "lovdesc", &valsize, &desc);
826
827                 rc = obd_init_ea_size(lmv_exp,
828                                       obd_size_diskmd(sbi->ll_lov_exp, NULL),
829                                       (desc.ld_tgt_count *
830                                        sizeof(struct llog_cookie)));
831         }
832         OBD_FREE(name, namelen);
833         RETURN(rc);
834 }
835
836 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
837 {
838         struct inode *inode = NULL;
839         l_lock(&lock->l_resource->lr_namespace->ns_lock);
840         if (lock->l_ast_data) {
841                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
842                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
843                         inode = igrab(lock->l_ast_data);
844                 } else {
845                         CERROR("DEBUG: l_ast_data %p is bogus: magic %x\n",
846                                lock->l_ast_data, lli->lli_inode_magic);
847                 }
848         }
849         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
850         return inode;
851 }
852
853 int null_if_equal(struct ldlm_lock *lock, void *data)
854 {
855         if (data == lock->l_ast_data) {
856                 lock->l_ast_data = NULL;
857
858                 if (lock->l_req_mode != lock->l_granted_mode)
859                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
860         }
861
862         return LDLM_ITER_CONTINUE;
863 }
864
865 void ll_clear_inode(struct inode *inode)
866 {
867         struct lustre_id id;
868         struct ll_inode_info *lli = ll_i2info(inode);
869         struct ll_sb_info *sbi = ll_i2sbi(inode);
870         ENTRY;
871
872         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
873                inode->i_generation, inode);
874
875         lli->lli_inode_magic = LLI_INODE_DEAD;
876         ll_inode2id(&id, inode);
877         
878         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
879         md_change_cbdata(sbi->ll_lmv_exp, &id, null_if_equal, inode);
880
881         if (lli->lli_smd)
882                 obd_change_cbdata(sbi->ll_lov_exp, lli->lli_smd,
883                                   null_if_equal, inode);
884
885         if (lli->lli_smd) {
886                 obd_free_memmd(sbi->ll_lov_exp, &lli->lli_smd);
887                 lli->lli_smd = NULL;
888         }
889
890         if (lli->lli_mea) {
891                 obd_free_memmd(sbi->ll_lmv_exp,
892                                (struct lov_stripe_md **) &lli->lli_mea);
893                 lli->lli_mea = NULL;
894         }
895
896         if (lli->lli_symlink_name) {
897                 OBD_FREE(lli->lli_symlink_name,
898                          strlen(lli->lli_symlink_name) + 1);
899                 lli->lli_symlink_name = NULL;
900         }
901
902         EXIT;
903 }
904
905 /* If this inode has objects allocated to it (lsm != NULL), then the OST
906  * object(s) determine the file size and mtime.  Otherwise, the MDS will
907  * keep these values until such a time that objects are allocated for it.
908  * We do the MDS operations first, as it is checking permissions for us.
909  * We don't to the MDS RPC if there is nothing that we want to store there,
910  * otherwise there is no harm in updating mtime/atime on the MDS if we are
911  * going to do an RPC anyways.
912  *
913  * If we are doing a truncate, we will send the mtime and ctime updates
914  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
915  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
916  * at the same time.
917  */
918 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
919 {
920         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
921         struct ll_sb_info *sbi = ll_i2sbi(inode);
922         struct ptlrpc_request *request = NULL;
923         struct mdc_op_data op_data;
924         int ia_valid = attr->ia_valid;
925         int rc = 0;
926         ENTRY;
927
928         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
929         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
930
931         if (ia_valid & ATTR_SIZE) {
932                 if (attr->ia_size > ll_file_maxbytes(inode)) {
933                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
934                                attr->ia_size, ll_file_maxbytes(inode));
935                         RETURN(-EFBIG);
936                 }
937
938                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
939         }
940
941         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
942         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
943                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
944                         RETURN(-EPERM);
945         }
946
947         /* We mark all of the fields "set" so MDS/OST does not re-set them */
948         if (attr->ia_valid & ATTR_CTIME) {
949                 attr->ia_ctime = CURRENT_TIME;
950                 attr->ia_valid |= ATTR_CTIME_SET;
951         }
952         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
953                 attr->ia_atime = CURRENT_TIME;
954                 attr->ia_valid |= ATTR_ATIME_SET;
955         }
956         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
957                 attr->ia_mtime = CURRENT_TIME;
958                 attr->ia_valid |= ATTR_MTIME_SET;
959         }
960
961         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
962                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
963                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
964                        LTIME_S(CURRENT_TIME));
965         if (lsm)
966                 attr->ia_valid &= ~ATTR_SIZE;
967
968         /* If only OST attributes being set on objects, don't do MDS RPC.
969          * In that case, we need to check permissions and update the local
970          * inode ourselves so we can call obdo_from_inode() always. */
971         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
972                 struct lustre_md md;
973                 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
974
975                 rc = md_setattr(sbi->ll_lmv_exp, &op_data,
976                                 attr, NULL, 0, NULL, 0, &request);
977                 if (rc) {
978                         ptlrpc_req_finished(request);
979                         if (rc != -EPERM && rc != -EACCES)
980                                 CERROR("md_setattr fails: rc = %d\n", rc);
981                         RETURN(rc);
982                 }
983
984                 rc = mdc_req2lustre_md(sbi->ll_lmv_exp, request, 0, 
985                                        sbi->ll_lov_exp, &md);
986                 if (rc) {
987                         ptlrpc_req_finished(request);
988                         RETURN(rc);
989                 }
990
991                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
992                  * but needed to set timestamps backwards on utime. */
993                 inode_setattr(inode, attr);
994                 ll_update_inode(inode, &md);
995                 ptlrpc_req_finished(request);
996
997                 if (!lsm || !S_ISREG(inode->i_mode)) {
998                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
999                         RETURN(0);
1000                 }
1001         } else {
1002                 /* The OST doesn't check permissions, but the alternative is
1003                  * a gratuitous RPC to the MDS.  We already rely on the client
1004                  * to do read/write/truncate permission checks, so is mtime OK?
1005                  */
1006                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1007                         /* from sys_utime() */
1008                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1009                                 if (current->fsuid != inode->i_uid &&
1010                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1011                                         RETURN(rc);
1012                         } else {
1013                                 /* from inode_change_ok() */
1014                                 if (current->fsuid != inode->i_uid &&
1015                                     !capable(CAP_FOWNER))
1016                                         RETURN(-EPERM);
1017                         }
1018                 }
1019
1020                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1021                 inode_setattr(inode, attr);
1022         }
1023
1024         /* We really need to get our PW lock before we change inode->i_size.
1025          * If we don't we can race with other i_size updaters on our node, like
1026          * ll_file_read.  We can also race with i_size propogation to other
1027          * nodes through dirtying and writeback of final cached pages.  This
1028          * last one is especially bad for racing o_append users on other
1029          * nodes. */
1030         if (ia_valid & ATTR_SIZE) {
1031                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1032                                                            OBD_OBJECT_EOF } };
1033                 struct lustre_handle lockh = { 0 };
1034                 int err, ast_flags = 0;
1035                 /* XXX when we fix the AST intents to pass the discard-range
1036                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1037                  * XXX here. */
1038                 if (attr->ia_size == 0)
1039                         ast_flags = LDLM_AST_DISCARD_DATA;
1040
1041                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
1042                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
1043                 up(&inode->i_sem);
1044                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1045                                     ast_flags, &ll_i2sbi(inode)->ll_seek_stime);
1046                 down(&inode->i_sem);
1047                 if (rc != 0)
1048                         RETURN(rc);
1049
1050                 rc = vmtruncate(inode, attr->ia_size);
1051
1052                 /* We need to drop the semaphore here, because this unlock may
1053                  * result in a cancellation, which will need the i_sem */
1054                 up(&inode->i_sem);
1055                 /* unlock now as we don't mind others file lockers racing with
1056                  * the mds updates below? */
1057                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1058                 down(&inode->i_sem);
1059                 if (err) {
1060                         CERROR("ll_extent_unlock failed: %d\n", err);
1061                         if (!rc)
1062                                 rc = err;
1063                 }
1064         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1065                 struct obdo oa;
1066
1067                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1068                        inode->i_ino, LTIME_S(attr->ia_mtime));
1069                 oa.o_id = lsm->lsm_object_id;
1070                 oa.o_gr = lsm->lsm_object_gr;
1071                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1072                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1073                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1074                 rc = obd_setattr(sbi->ll_lov_exp, &oa, lsm, NULL);
1075                 if (rc)
1076                         CERROR("obd_setattr fails: rc=%d\n", rc);
1077         }
1078         RETURN(rc);
1079 }
1080
1081 int ll_setattr(struct dentry *de, struct iattr *attr)
1082 {
1083         LBUG(); /* code is unused, but leave this in case of VFS changes */
1084         RETURN(-ENOSYS);
1085 }
1086
1087 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1088                        unsigned long max_age)
1089 {
1090         struct ll_sb_info *sbi = ll_s2sbi(sb);
1091         struct obd_statfs obd_osfs;
1092         int rc;
1093         ENTRY;
1094
1095         rc = obd_statfs(class_exp2obd(sbi->ll_lmv_exp), osfs, max_age);
1096         if (rc) {
1097                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1098                 RETURN(rc);
1099         }
1100
1101         osfs->os_type = sb->s_magic;
1102
1103         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1104                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1105
1106         rc = obd_statfs(class_exp2obd(sbi->ll_lov_exp), &obd_osfs, max_age);
1107         if (rc) {
1108                 CERROR("obd_statfs fails: rc = %d\n", rc);
1109                 RETURN(rc);
1110         }
1111
1112         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1113                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1114                obd_osfs.os_files);
1115
1116         osfs->os_blocks = obd_osfs.os_blocks;
1117         osfs->os_bfree = obd_osfs.os_bfree;
1118         osfs->os_bavail = obd_osfs.os_bavail;
1119
1120         /* If we don't have as many objects free on the OST as inodes
1121          * on the MDS, we reduce the total number of inodes to
1122          * compensate, so that the "inodes in use" number is correct.
1123          */
1124         if (obd_osfs.os_ffree < osfs->os_ffree) {
1125                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1126                         obd_osfs.os_ffree;
1127                 osfs->os_ffree = obd_osfs.os_ffree;
1128         }
1129
1130         RETURN(rc);
1131 }
1132
1133 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1134 {
1135         struct obd_statfs osfs;
1136         int rc;
1137
1138         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p\n", sb);
1139         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1140
1141         /* For now we will always get up-to-date statfs values, but in the
1142          * future we may allow some amount of caching on the client (e.g.
1143          * from QOS or lprocfs updates). */
1144         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1145         if (rc)
1146                 return rc;
1147
1148         statfs_unpack(sfs, &osfs);
1149
1150         if (sizeof(sfs->f_blocks) == 4) {
1151                 while (osfs.os_blocks > ~0UL) {
1152                         sfs->f_bsize <<= 1;
1153
1154                         osfs.os_blocks >>= 1;
1155                         osfs.os_bfree >>= 1;
1156                         osfs.os_bavail >>= 1;
1157                 }
1158         }
1159
1160         sfs->f_blocks = osfs.os_blocks;
1161         sfs->f_bfree = osfs.os_bfree;
1162         sfs->f_bavail = osfs.os_bavail;
1163
1164         return 0;
1165 }
1166
1167 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1168 {
1169         struct ll_sb_info *sbi = ll_s2sbi(inode->i_sb);
1170         struct ll_inode_info *lli = ll_i2info(inode);
1171         struct lov_stripe_md *lsm = md->lsm;
1172         struct mds_body *body = md->body;
1173         struct mea *mea = md->mea;
1174         ENTRY;
1175
1176         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1177         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1178         if (lsm != NULL) {
1179                 LASSERT(lsm->lsm_object_gr > 0);
1180                 if (lli->lli_smd == NULL) {
1181                         lli->lli_smd = lsm;
1182                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1183                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1184                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1185                 } else {
1186                         int i;
1187                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1188                                 CERROR("lsm mismatch for inode %ld\n",
1189                                        inode->i_ino);
1190                                 CERROR("lli_smd:\n");
1191                                 dump_lsm(D_ERROR, lli->lli_smd);
1192                                 CERROR("lsm:\n");
1193                                 dump_lsm(D_ERROR, lsm);
1194                                 LBUG();
1195                         }
1196                         /* XXX FIXME -- We should decide on a safer (atomic) and
1197                          * more elegant way to update the lsm */
1198                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1199                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1200                                         lsm->lsm_oinfo[i].loi_id;
1201                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1202                                         lsm->lsm_oinfo[i].loi_gr;
1203                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1204                                         lsm->lsm_oinfo[i].loi_ost_idx;
1205                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1206                                         lsm->lsm_oinfo[i].loi_ost_gen;
1207                         }
1208                 }
1209                 /* bug 2844 - limit i_blksize for broken user-space apps */
1210                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1211                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1212                 if (lli->lli_smd != lsm)
1213                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1214         }
1215
1216         if (mea != NULL) {
1217                 if (lli->lli_mea == NULL) {
1218                         lli->lli_mea = mea;
1219                 } else {
1220                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1221                                 CERROR("mea mismatch for inode %lu\n",
1222                                         inode->i_ino);
1223                                 LBUG();
1224                         }
1225                 }
1226                 if (lli->lli_mea != mea)
1227                         obd_free_memmd(ll_i2lmvexp(inode),
1228                                        (struct lov_stripe_md **) &mea);
1229         }
1230
1231         /*
1232          * updating inode lustre id. We try optimize things a little bit here,
1233          * thus do not do it all the time.
1234          */
1235         if (body->valid & OBD_MD_FID) {
1236                 LASSERT(id_fid(&body->id1) != 0);
1237                 id_assign_fid(&lli->lli_id, &body->id1);
1238         }
1239         
1240         if ((body->valid & OBD_MD_FLID) || (body->valid & OBD_MD_FLGENER))
1241                 id_assign_stc(&lli->lli_id, &body->id1);
1242
1243         if (body->valid & OBD_MD_FLID)
1244                 inode->i_ino = id_ino(&body->id1);
1245         if (body->valid & OBD_MD_FLGENER)
1246                 inode->i_generation = id_gen(&body->id1);
1247         if (body->valid & OBD_MD_FLATIME)
1248                 LTIME_S(inode->i_atime) = body->atime;
1249         if (body->valid & OBD_MD_FLMTIME &&
1250             body->mtime > LTIME_S(inode->i_mtime)) {
1251                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1252                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1253                 LTIME_S(inode->i_mtime) = body->mtime;
1254         }
1255         if (body->valid & OBD_MD_FLCTIME &&
1256             body->ctime > LTIME_S(inode->i_ctime))
1257                 LTIME_S(inode->i_ctime) = body->ctime;
1258         if (body->valid & OBD_MD_FLMODE) {
1259                 inode->i_mode = (inode->i_mode & S_IFMT) |
1260                         (body->mode & ~S_IFMT);
1261         }
1262         if (body->valid & OBD_MD_FLTYPE) {
1263                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1264                         (body->mode & S_IFMT);
1265         }
1266         if (body->valid & OBD_MD_FLUID)
1267                 inode->i_uid = body->uid;
1268         if (body->valid & OBD_MD_FLGID)
1269                 inode->i_gid = body->gid;
1270         if (body->valid & OBD_MD_FLFLAGS)
1271                 inode->i_flags = body->flags;
1272         if (body->valid & OBD_MD_FLNLINK)
1273                 inode->i_nlink = body->nlink;
1274         if (body->valid & OBD_MD_FLRDEV)
1275 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1276                 inode->i_rdev = body->rdev;
1277 #else
1278                 inode->i_rdev = old_decode_dev(body->rdev);
1279 #endif
1280         if (body->valid & OBD_MD_FLSIZE)
1281                 inode->i_size = body->size;
1282         if (body->valid & OBD_MD_FLBLOCKS)
1283                 inode->i_blocks = body->blocks;
1284
1285         if (body->valid & OBD_MD_FLSIZE)
1286                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1287
1288 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1289         inode->i_dev = (kdev_t)id_group(&lli->lli_id);
1290 #endif
1291         LASSERT(id_fid(&lli->lli_id) != 0);
1292         
1293         LASSERT(!sbi->ll_lmv_desc.ld_tgt_count ||
1294                 id_group(&lli->lli_id) < sbi->ll_lmv_desc.ld_tgt_count);
1295 }
1296
1297 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1298 static struct backing_dev_info ll_backing_dev_info = {
1299         .ra_pages       = 0,    /* No readahead */
1300         .memory_backed  = 0,    /* Does contribute to dirty memory */
1301 };
1302 #endif
1303
1304 void ll_read_inode2(struct inode *inode, void *opaque)
1305 {
1306         struct lustre_md *md = opaque;
1307         struct ll_inode_info *lli = ll_i2info(inode);
1308         ENTRY;
1309
1310         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1311                inode->i_generation, inode);
1312
1313         ll_lli_init(lli);
1314
1315         LASSERT(!lli->lli_smd);
1316
1317         /* Core attributes from the MDS first.  This is a new inode, and
1318          * the VFS doesn't zero times in the core inode so we have to do
1319          * it ourselves.  They will be overwritten by either MDS or OST
1320          * attributes - we just need to make sure they aren't newer. */
1321         LTIME_S(inode->i_mtime) = 0;
1322         LTIME_S(inode->i_atime) = 0;
1323         LTIME_S(inode->i_ctime) = 0;
1324
1325         inode->i_rdev = 0;
1326         ll_update_inode(inode, md);
1327
1328         /* OIDEBUG(inode); */
1329
1330         if (S_ISREG(inode->i_mode)) {
1331                 inode->i_op = &ll_file_inode_operations;
1332                 inode->i_fop = &ll_file_operations;
1333                 inode->i_mapping->a_ops = &ll_aops;
1334                 EXIT;
1335         } else if (S_ISDIR(inode->i_mode)) {
1336                 inode->i_op = &ll_dir_inode_operations;
1337                 inode->i_fop = &ll_dir_operations;
1338                 inode->i_mapping->a_ops = &ll_dir_aops;
1339                 EXIT;
1340         } else if (S_ISLNK(inode->i_mode)) {
1341                 inode->i_op = &ll_fast_symlink_inode_operations;
1342                 EXIT;
1343         } else {
1344                 inode->i_op = &ll_special_inode_operations;
1345
1346 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1347                 init_special_inode(inode, inode->i_mode,
1348                                    kdev_t_to_nr(inode->i_rdev));
1349
1350                 /* initializing backing dev info. */
1351                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1352 #else
1353                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1354 #endif
1355                 lli->ll_save_ifop = inode->i_fop;
1356
1357                 if (S_ISCHR(inode->i_mode))
1358                         inode->i_fop = &ll_special_chr_inode_fops;
1359                 else if (S_ISBLK(inode->i_mode))
1360                         inode->i_fop = &ll_special_blk_inode_fops;
1361                 else if (S_ISFIFO(inode->i_mode))
1362                         inode->i_fop = &ll_special_fifo_inode_fops;
1363                 else if (S_ISSOCK(inode->i_mode))
1364                         inode->i_fop = &ll_special_sock_inode_fops;
1365
1366                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1367                       inode->i_fop);
1368
1369                 if (lli->ll_save_ifop->owner) {
1370                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1371                               lli->ll_save_ifop->owner);
1372                 }
1373                 EXIT;
1374         }
1375 }
1376
1377 void ll_put_inode(struct inode *inode)
1378 {
1379         struct ll_sb_info *sbi = ll_i2sbi(inode);
1380         struct lustre_id id;
1381         int rc;
1382         ENTRY;
1383
1384         /* notifying metadata target of putting inode. */
1385         if (atomic_read(&inode->i_count) == 1) {
1386                 ll_inode2id(&id, inode);
1387
1388                 rc = md_put_inode(sbi->ll_lmv_exp, &id);
1389                 if (rc) {
1390                         CERROR("md_put_inode() failed, "
1391                                "error %d.\n", rc);
1392                 }
1393         }
1394
1395         EXIT;
1396 }
1397
1398 int ll_iocontrol(struct inode *inode, struct file *file,
1399                  unsigned int cmd, unsigned long arg)
1400 {
1401         struct ll_sb_info *sbi = ll_i2sbi(inode);
1402         struct ptlrpc_request *req = NULL;
1403         int rc, flags = 0;
1404         ENTRY;
1405
1406         switch(cmd) {
1407         case EXT3_IOC_GETFLAGS: {
1408                 struct lustre_id id;
1409                 unsigned long valid = OBD_MD_FLFLAGS;
1410                 struct mds_body *body;
1411
1412                 ll_inode2id(&id, inode);
1413                 rc = md_getattr(sbi->ll_lmv_exp, &id, valid, 0, &req);
1414                 if (rc) {
1415                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1416                         RETURN(-abs(rc));
1417                 }
1418
1419                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1420
1421                 if (body->flags & S_APPEND)
1422                         flags |= EXT3_APPEND_FL;
1423                 if (body->flags & S_IMMUTABLE)
1424                         flags |= EXT3_IMMUTABLE_FL;
1425                 if (body->flags & S_NOATIME)
1426                         flags |= EXT3_NOATIME_FL;
1427
1428                 ptlrpc_req_finished (req);
1429
1430                 RETURN(put_user(flags, (int *)arg));
1431         }
1432         case EXT3_IOC_SETFLAGS: {
1433                 struct mdc_op_data op_data;
1434                 struct iattr attr;
1435                 struct obdo *oa;
1436                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1437
1438                 if (get_user(flags, (int *)arg))
1439                         RETURN(-EFAULT);
1440
1441                 oa = obdo_alloc();
1442                 if (!oa)
1443                         RETURN(-ENOMEM);
1444
1445                 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
1446
1447                 memset(&attr, 0x0, sizeof(attr));
1448                 attr.ia_attr_flags = flags;
1449                 attr.ia_valid |= ATTR_ATTR_FLAG;
1450
1451                 rc = md_setattr(sbi->ll_lmv_exp, &op_data,
1452                                 &attr, NULL, 0, NULL, 0, &req);
1453                 if (rc) {
1454                         ptlrpc_req_finished(req);
1455                         if (rc != -EPERM && rc != -EACCES)
1456                                 CERROR("md_setattr fails: rc = %d\n", rc);
1457                         obdo_free(oa);
1458                         RETURN(rc);
1459                 }
1460                 ptlrpc_req_finished(req);
1461
1462                 oa->o_id = lsm->lsm_object_id;
1463                 oa->o_gr = lsm->lsm_object_gr;
1464                 oa->o_flags = flags;
1465                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1466
1467                 rc = obd_setattr(sbi->ll_lov_exp, oa, lsm, NULL);
1468                 obdo_free(oa);
1469                 if (rc) {
1470                         if (rc != -EPERM && rc != -EACCES)
1471                                 CERROR("md_setattr fails: rc = %d\n", rc);
1472                         RETURN(rc);
1473                 }
1474
1475                 if (flags & EXT3_APPEND_FL)
1476                         inode->i_flags |= S_APPEND;
1477                 else
1478                         inode->i_flags &= ~S_APPEND;
1479                 if (flags & EXT3_IMMUTABLE_FL)
1480                         inode->i_flags |= S_IMMUTABLE;
1481                 else
1482                         inode->i_flags &= ~S_IMMUTABLE;
1483                 if (flags & EXT3_NOATIME_FL)
1484                         inode->i_flags |= S_NOATIME;
1485                 else
1486                         inode->i_flags &= ~S_NOATIME;
1487
1488                 RETURN(0);
1489         }
1490         default:
1491                 RETURN(-ENOSYS);
1492         }
1493
1494         RETURN(0);
1495 }
1496
1497 /* this is only called in the case of forced umount. */
1498 void ll_umount_begin(struct super_block *sb)
1499 {
1500         struct ll_sb_info *sbi = ll_s2sbi(sb);
1501         struct obd_device *obd;
1502         struct obd_ioctl_data ioc_data = { 0 };
1503         ENTRY;
1504      
1505         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1506                sb->s_count, atomic_read(&sb->s_active));
1507         
1508         obd = class_exp2obd(sbi->ll_lmv_exp);
1509         if (obd == NULL) {
1510                 CERROR("Invalid MDC connection handle "LPX64"\n",
1511                        sbi->ll_lmv_exp->exp_handle.h_cookie);
1512                 EXIT;
1513                 return;
1514         }
1515         obd->obd_no_recov = 1;
1516         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lmv_exp,
1517                       sizeof(ioc_data), &ioc_data, NULL);
1518
1519         obd = class_exp2obd(sbi->ll_lov_exp);
1520         if (obd == NULL) {
1521                 CERROR("Invalid LOV connection handle "LPX64"\n",
1522                        sbi->ll_lov_exp->exp_handle.h_cookie);
1523                 EXIT;
1524                 return;
1525         }
1526
1527         obd->obd_no_recov = 1;
1528         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lov_exp,
1529                       sizeof(ioc_data), &ioc_data, NULL);
1530
1531         /*
1532          * really, we'd like to wait until there are no requests outstanding,
1533          * and then continue.  For now, we just invalidate the requests,
1534          * schedule, and hope.
1535          */
1536         schedule();
1537
1538         EXIT;
1539 }
1540
1541 int ll_prep_inode(struct obd_export *lov_exp, struct obd_export *lmv_exp,
1542                   struct inode **inode, struct ptlrpc_request *req,
1543                   int offset, struct super_block *sb)
1544 {
1545         struct lustre_md md;
1546         int rc = 0;
1547
1548         rc = mdc_req2lustre_md(lmv_exp, req, offset, lov_exp, &md);
1549         if (rc)
1550                 RETURN(rc);
1551
1552         if (*inode) {
1553                 ll_update_inode(*inode, &md);
1554         } else {
1555                 LASSERT(sb);
1556                 *inode = ll_iget(sb, id_ino(&md.body->id1), &md);
1557                 if (*inode == NULL || is_bad_inode(*inode)) {
1558                         /* free the lsm if we allocated one above */
1559                         if (md.lsm != NULL)
1560                                 obd_free_memmd(lov_exp, &md.lsm);
1561                         if (md.mea != NULL)
1562                                 obd_free_memmd(lmv_exp,
1563                                                (struct lov_stripe_md**)&md.mea);
1564                         rc = -ENOMEM;
1565                         CERROR("new_inode -fatal: rc %d\n", rc);
1566                 }
1567         }
1568
1569         RETURN(rc);
1570 }
1571
1572 int ll_get_fid(struct obd_export *exp, struct lustre_id *idp,
1573                char *filename, struct lustre_id *ret)
1574 {
1575         struct ptlrpc_request *request = NULL;
1576         struct mds_body *body;
1577         int valid = 0;
1578         int rc;
1579
1580         valid |= OBD_MD_FID;
1581         
1582         rc = md_getattr_lock(exp, idp, filename, strlen(filename) + 1,
1583                              valid, 0, &request);
1584         if (rc < 0) {
1585                 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
1586                        filename, rc);
1587                 return rc;
1588         }
1589
1590         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1591         LASSERT(body != NULL);
1592         LASSERT_REPSWABBED(request, 0);
1593
1594         *ret = body->id1;
1595         ptlrpc_req_finished(request);
1596
1597         return rc;
1598 }