Whamcloud - gitweb
961f49168d1bcbe0e0f4cfc497d908cd591b3d53
[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
974                 OBD_ALLOC(op_data, sizeof(*op_data));
975                 if (op_data == NULL)
976                         RETURN(-ENOMEM);
977                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
978
979                 rc = md_setattr(sbi->ll_lmv_exp, op_data,
980                                 attr, NULL, 0, NULL, 0, &request);
981                 OBD_FREE(op_data, sizeof(*op_data));
982                 if (rc) {
983                         ptlrpc_req_finished(request);
984                         if (rc != -EPERM && rc != -EACCES)
985                                 CERROR("md_setattr fails: rc = %d\n", rc);
986                         RETURN(rc);
987                 }
988
989                 rc = mdc_req2lustre_md(sbi->ll_lmv_exp, request, 0, 
990                                        sbi->ll_lov_exp, &md);
991                 if (rc) {
992                         ptlrpc_req_finished(request);
993                         RETURN(rc);
994                 }
995
996                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
997                  * but needed to set timestamps backwards on utime. */
998                 inode_setattr(inode, attr);
999                 ll_update_inode(inode, &md);
1000                 ptlrpc_req_finished(request);
1001
1002                 if (!lsm || !S_ISREG(inode->i_mode)) {
1003                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1004                         RETURN(0);
1005                 }
1006         } else {
1007                 /* The OST doesn't check permissions, but the alternative is
1008                  * a gratuitous RPC to the MDS.  We already rely on the client
1009                  * to do read/write/truncate permission checks, so is mtime OK?
1010                  */
1011                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1012                         /* from sys_utime() */
1013                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1014                                 if (current->fsuid != inode->i_uid &&
1015                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1016                                         RETURN(rc);
1017                         } else {
1018                                 /* from inode_change_ok() */
1019                                 if (current->fsuid != inode->i_uid &&
1020                                     !capable(CAP_FOWNER))
1021                                         RETURN(-EPERM);
1022                         }
1023                 }
1024
1025                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1026                 inode_setattr(inode, attr);
1027         }
1028
1029         /* We really need to get our PW lock before we change inode->i_size.
1030          * If we don't we can race with other i_size updaters on our node, like
1031          * ll_file_read.  We can also race with i_size propogation to other
1032          * nodes through dirtying and writeback of final cached pages.  This
1033          * last one is especially bad for racing o_append users on other
1034          * nodes. */
1035         if (ia_valid & ATTR_SIZE) {
1036                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1037                                                            OBD_OBJECT_EOF } };
1038                 struct lustre_handle lockh = { 0 };
1039                 int err, ast_flags = 0;
1040                 /* XXX when we fix the AST intents to pass the discard-range
1041                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1042                  * XXX here. */
1043                 if (attr->ia_size == 0)
1044                         ast_flags = LDLM_AST_DISCARD_DATA;
1045
1046                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
1047                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
1048                 up(&inode->i_sem);
1049                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1050                                     ast_flags, &ll_i2sbi(inode)->ll_seek_stime);
1051                 down(&inode->i_sem);
1052                 if (rc != 0)
1053                         RETURN(rc);
1054
1055                 rc = vmtruncate(inode, attr->ia_size);
1056
1057                 /* We need to drop the semaphore here, because this unlock may
1058                  * result in a cancellation, which will need the i_sem */
1059                 up(&inode->i_sem);
1060                 /* unlock now as we don't mind others file lockers racing with
1061                  * the mds updates below? */
1062                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1063                 down(&inode->i_sem);
1064                 if (err) {
1065                         CERROR("ll_extent_unlock failed: %d\n", err);
1066                         if (!rc)
1067                                 rc = err;
1068                 }
1069         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1070                 struct obdo *oa = NULL;
1071
1072                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1073                        inode->i_ino, LTIME_S(attr->ia_mtime));
1074
1075                 oa = obdo_alloc();
1076                 if (oa == NULL)
1077                         RETURN(-ENOMEM);
1078
1079                 oa->o_id = lsm->lsm_object_id;
1080                 oa->o_gr = lsm->lsm_object_gr;
1081                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1082                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1083                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1084                 rc = obd_setattr(sbi->ll_lov_exp, oa, lsm, NULL);
1085                 obdo_free(oa);
1086                 if (rc)
1087                         CERROR("obd_setattr fails: rc = %d\n", rc);
1088         }
1089         RETURN(rc);
1090 }
1091
1092 int ll_setattr(struct dentry *de, struct iattr *attr)
1093 {
1094         LBUG(); /* code is unused, but leave this in case of VFS changes */
1095         RETURN(-ENOSYS);
1096 }
1097
1098 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1099                        unsigned long max_age)
1100 {
1101         struct ll_sb_info *sbi = ll_s2sbi(sb);
1102         struct obd_statfs obd_osfs;
1103         int rc;
1104         ENTRY;
1105
1106         rc = obd_statfs(class_exp2obd(sbi->ll_lmv_exp), osfs, max_age);
1107         if (rc) {
1108                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1109                 RETURN(rc);
1110         }
1111
1112         osfs->os_type = sb->s_magic;
1113
1114         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1115                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1116
1117         rc = obd_statfs(class_exp2obd(sbi->ll_lov_exp), &obd_osfs, max_age);
1118         if (rc) {
1119                 CERROR("obd_statfs fails: rc = %d\n", rc);
1120                 RETURN(rc);
1121         }
1122
1123         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1124                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1125                obd_osfs.os_files);
1126
1127         osfs->os_blocks = obd_osfs.os_blocks;
1128         osfs->os_bfree = obd_osfs.os_bfree;
1129         osfs->os_bavail = obd_osfs.os_bavail;
1130
1131         /* If we don't have as many objects free on the OST as inodes
1132          * on the MDS, we reduce the total number of inodes to
1133          * compensate, so that the "inodes in use" number is correct.
1134          */
1135         if (obd_osfs.os_ffree < osfs->os_ffree) {
1136                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1137                         obd_osfs.os_ffree;
1138                 osfs->os_ffree = obd_osfs.os_ffree;
1139         }
1140
1141         RETURN(rc);
1142 }
1143
1144 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1145 {
1146         struct obd_statfs osfs;
1147         int rc;
1148
1149         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p\n", sb);
1150         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1151
1152         /* For now we will always get up-to-date statfs values, but in the
1153          * future we may allow some amount of caching on the client (e.g.
1154          * from QOS or lprocfs updates). */
1155         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1156         if (rc)
1157                 return rc;
1158
1159         statfs_unpack(sfs, &osfs);
1160
1161         if (sizeof(sfs->f_blocks) == 4) {
1162                 while (osfs.os_blocks > ~0UL) {
1163                         sfs->f_bsize <<= 1;
1164
1165                         osfs.os_blocks >>= 1;
1166                         osfs.os_bfree >>= 1;
1167                         osfs.os_bavail >>= 1;
1168                 }
1169         }
1170
1171         sfs->f_blocks = osfs.os_blocks;
1172         sfs->f_bfree = osfs.os_bfree;
1173         sfs->f_bavail = osfs.os_bavail;
1174
1175         return 0;
1176 }
1177
1178 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1179 {
1180         struct ll_sb_info *sbi = ll_s2sbi(inode->i_sb);
1181         struct ll_inode_info *lli = ll_i2info(inode);
1182         struct lov_stripe_md *lsm = md->lsm;
1183         struct mds_body *body = md->body;
1184         struct mea *mea = md->mea;
1185         ENTRY;
1186
1187         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1188         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1189         if (lsm != NULL) {
1190                 LASSERT(lsm->lsm_object_gr > 0);
1191                 if (lli->lli_smd == NULL) {
1192                         lli->lli_smd = lsm;
1193                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1194                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1195                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1196                 } else {
1197                         int i;
1198                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1199                                 CERROR("lsm mismatch for inode %ld\n",
1200                                        inode->i_ino);
1201                                 CERROR("lli_smd:\n");
1202                                 dump_lsm(D_ERROR, lli->lli_smd);
1203                                 CERROR("lsm:\n");
1204                                 dump_lsm(D_ERROR, lsm);
1205                                 LBUG();
1206                         }
1207                         /* XXX FIXME -- We should decide on a safer (atomic) and
1208                          * more elegant way to update the lsm */
1209                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1210                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1211                                         lsm->lsm_oinfo[i].loi_id;
1212                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1213                                         lsm->lsm_oinfo[i].loi_gr;
1214                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1215                                         lsm->lsm_oinfo[i].loi_ost_idx;
1216                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1217                                         lsm->lsm_oinfo[i].loi_ost_gen;
1218                         }
1219                 }
1220                 /* bug 2844 - limit i_blksize for broken user-space apps */
1221                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1222                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1223                 if (lli->lli_smd != lsm)
1224                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1225         }
1226
1227         if (mea != NULL) {
1228                 if (lli->lli_mea == NULL) {
1229                         lli->lli_mea = mea;
1230                 } else {
1231                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1232                                 CERROR("mea mismatch for inode %lu\n",
1233                                         inode->i_ino);
1234                                 LBUG();
1235                         }
1236                 }
1237                 if (lli->lli_mea != mea)
1238                         obd_free_memmd(ll_i2lmvexp(inode),
1239                                        (struct lov_stripe_md **) &mea);
1240         }
1241
1242         /*
1243          * updating inode lustre id. We try optimize things a little bit here,
1244          * thus do not do it all the time.
1245          */
1246         if (body->valid & OBD_MD_FID) {
1247                 LASSERT(id_fid(&body->id1) != 0);
1248                 id_assign_fid(&lli->lli_id, &body->id1);
1249         }
1250         
1251         if ((body->valid & OBD_MD_FLID) || (body->valid & OBD_MD_FLGENER))
1252                 id_assign_stc(&lli->lli_id, &body->id1);
1253
1254         if (body->valid & OBD_MD_FLID)
1255                 inode->i_ino = id_ino(&body->id1);
1256         if (body->valid & OBD_MD_FLGENER)
1257                 inode->i_generation = id_gen(&body->id1);
1258         if (body->valid & OBD_MD_FLATIME)
1259                 LTIME_S(inode->i_atime) = body->atime;
1260         if (body->valid & OBD_MD_FLMTIME &&
1261             body->mtime > LTIME_S(inode->i_mtime)) {
1262                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1263                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1264                 LTIME_S(inode->i_mtime) = body->mtime;
1265         }
1266         if (body->valid & OBD_MD_FLCTIME &&
1267             body->ctime > LTIME_S(inode->i_ctime))
1268                 LTIME_S(inode->i_ctime) = body->ctime;
1269         if (body->valid & OBD_MD_FLMODE) {
1270                 inode->i_mode = (inode->i_mode & S_IFMT) |
1271                         (body->mode & ~S_IFMT);
1272         }
1273         if (body->valid & OBD_MD_FLTYPE) {
1274                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
1275                         (body->mode & S_IFMT);
1276         }
1277         if (body->valid & OBD_MD_FLUID)
1278                 inode->i_uid = body->uid;
1279         if (body->valid & OBD_MD_FLGID)
1280                 inode->i_gid = body->gid;
1281         if (body->valid & OBD_MD_FLFLAGS)
1282                 inode->i_flags = body->flags;
1283         if (body->valid & OBD_MD_FLNLINK)
1284                 inode->i_nlink = body->nlink;
1285         if (body->valid & OBD_MD_FLRDEV)
1286 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1287                 inode->i_rdev = body->rdev;
1288 #else
1289                 inode->i_rdev = old_decode_dev(body->rdev);
1290 #endif
1291         if (body->valid & OBD_MD_FLSIZE)
1292                 inode->i_size = body->size;
1293         if (body->valid & OBD_MD_FLBLOCKS)
1294                 inode->i_blocks = body->blocks;
1295
1296         if (body->valid & OBD_MD_FLSIZE)
1297                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1298
1299 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1300         inode->i_dev = (kdev_t)id_group(&lli->lli_id);
1301 #endif
1302         LASSERT(id_fid(&lli->lli_id) != 0);
1303         
1304         LASSERT(!sbi->ll_lmv_desc.ld_tgt_count ||
1305                 id_group(&lli->lli_id) < sbi->ll_lmv_desc.ld_tgt_count);
1306 }
1307
1308 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1309 static struct backing_dev_info ll_backing_dev_info = {
1310         .ra_pages       = 0,    /* No readahead */
1311         .memory_backed  = 0,    /* Does contribute to dirty memory */
1312 };
1313 #endif
1314
1315 void ll_read_inode2(struct inode *inode, void *opaque)
1316 {
1317         struct lustre_md *md = opaque;
1318         struct ll_inode_info *lli = ll_i2info(inode);
1319         ENTRY;
1320
1321         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1322                inode->i_generation, inode);
1323
1324         ll_lli_init(lli);
1325
1326         LASSERT(!lli->lli_smd);
1327
1328         /* Core attributes from the MDS first.  This is a new inode, and
1329          * the VFS doesn't zero times in the core inode so we have to do
1330          * it ourselves.  They will be overwritten by either MDS or OST
1331          * attributes - we just need to make sure they aren't newer. */
1332         LTIME_S(inode->i_mtime) = 0;
1333         LTIME_S(inode->i_atime) = 0;
1334         LTIME_S(inode->i_ctime) = 0;
1335
1336         inode->i_rdev = 0;
1337         ll_update_inode(inode, md);
1338
1339         /* OIDEBUG(inode); */
1340
1341         if (S_ISREG(inode->i_mode)) {
1342                 inode->i_op = &ll_file_inode_operations;
1343                 inode->i_fop = &ll_file_operations;
1344                 inode->i_mapping->a_ops = &ll_aops;
1345                 EXIT;
1346         } else if (S_ISDIR(inode->i_mode)) {
1347                 inode->i_op = &ll_dir_inode_operations;
1348                 inode->i_fop = &ll_dir_operations;
1349                 inode->i_mapping->a_ops = &ll_dir_aops;
1350                 EXIT;
1351         } else if (S_ISLNK(inode->i_mode)) {
1352                 inode->i_op = &ll_fast_symlink_inode_operations;
1353                 EXIT;
1354         } else {
1355                 inode->i_op = &ll_special_inode_operations;
1356
1357 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1358                 init_special_inode(inode, inode->i_mode,
1359                                    kdev_t_to_nr(inode->i_rdev));
1360
1361                 /* initializing backing dev info. */
1362                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1363 #else
1364                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1365 #endif
1366                 lli->ll_save_ifop = inode->i_fop;
1367
1368                 if (S_ISCHR(inode->i_mode))
1369                         inode->i_fop = &ll_special_chr_inode_fops;
1370                 else if (S_ISBLK(inode->i_mode))
1371                         inode->i_fop = &ll_special_blk_inode_fops;
1372                 else if (S_ISFIFO(inode->i_mode))
1373                         inode->i_fop = &ll_special_fifo_inode_fops;
1374                 else if (S_ISSOCK(inode->i_mode))
1375                         inode->i_fop = &ll_special_sock_inode_fops;
1376
1377                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1378                       inode->i_fop);
1379
1380                 if (lli->ll_save_ifop->owner) {
1381                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1382                               lli->ll_save_ifop->owner);
1383                 }
1384                 EXIT;
1385         }
1386 }
1387
1388 void ll_delete_inode(struct inode *inode)
1389 {
1390         struct ll_sb_info *sbi = ll_i2sbi(inode);
1391         struct lustre_id id;
1392         int rc;
1393         ENTRY;
1394
1395         ll_inode2id(&id, inode);
1396
1397         rc = md_delete_inode(sbi->ll_lmv_exp, &id);
1398         if (rc) {
1399                 CERROR("md_delete_inode() failed, error %d\n", 
1400                        rc);
1401         }
1402
1403         clear_inode(inode);
1404         EXIT;
1405 }
1406
1407 int ll_iocontrol(struct inode *inode, struct file *file,
1408                  unsigned int cmd, unsigned long arg)
1409 {
1410         struct ll_sb_info *sbi = ll_i2sbi(inode);
1411         struct ptlrpc_request *req = NULL;
1412         int rc, flags = 0;
1413         ENTRY;
1414
1415         switch(cmd) {
1416         case EXT3_IOC_GETFLAGS: {
1417                 struct lustre_id id;
1418                 __u64 valid = OBD_MD_FLFLAGS;
1419                 struct mds_body *body;
1420
1421                 ll_inode2id(&id, inode);
1422                 rc = md_getattr(sbi->ll_lmv_exp, &id, valid, 0, &req);
1423                 if (rc) {
1424                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1425                         RETURN(-abs(rc));
1426                 }
1427
1428                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1429
1430                 if (body->flags & S_APPEND)
1431                         flags |= EXT3_APPEND_FL;
1432                 if (body->flags & S_IMMUTABLE)
1433                         flags |= EXT3_IMMUTABLE_FL;
1434                 if (body->flags & S_NOATIME)
1435                         flags |= EXT3_NOATIME_FL;
1436
1437                 ptlrpc_req_finished (req);
1438
1439                 RETURN(put_user(flags, (int *)arg));
1440         }
1441         case EXT3_IOC_SETFLAGS: {
1442                 struct mdc_op_data *op_data;
1443                 struct iattr attr;
1444                 struct obdo *oa;
1445                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1446
1447                 if (get_user(flags, (int *)arg))
1448                         RETURN(-EFAULT);
1449
1450                 oa = obdo_alloc();
1451                 if (!oa)
1452                         RETURN(-ENOMEM);
1453
1454                 OBD_ALLOC(op_data, sizeof(*op_data));
1455                 if (op_data == NULL) {
1456                         obdo_free(oa);
1457                         RETURN(-ENOMEM);
1458                 }
1459                 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
1460
1461                 memset(&attr, 0x0, sizeof(attr));
1462                 attr.ia_attr_flags = flags;
1463                 attr.ia_valid |= ATTR_ATTR_FLAG;
1464
1465                 rc = md_setattr(sbi->ll_lmv_exp, op_data,
1466                                 &attr, NULL, 0, NULL, 0, &req);
1467                 OBD_FREE(op_data, sizeof(*op_data));
1468                 if (rc) {
1469                         ptlrpc_req_finished(req);
1470                         if (rc != -EPERM && rc != -EACCES)
1471                                 CERROR("md_setattr fails: rc = %d\n", rc);
1472                         obdo_free(oa);
1473                         RETURN(rc);
1474                 }
1475                 ptlrpc_req_finished(req);
1476
1477                 oa->o_id = lsm->lsm_object_id;
1478                 oa->o_gr = lsm->lsm_object_gr;
1479                 oa->o_flags = flags;
1480                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1481
1482                 rc = obd_setattr(sbi->ll_lov_exp, oa, lsm, NULL);
1483                 obdo_free(oa);
1484                 if (rc) {
1485                         if (rc != -EPERM && rc != -EACCES)
1486                                 CERROR("md_setattr fails: rc = %d\n", rc);
1487                         RETURN(rc);
1488                 }
1489
1490                 if (flags & EXT3_APPEND_FL)
1491                         inode->i_flags |= S_APPEND;
1492                 else
1493                         inode->i_flags &= ~S_APPEND;
1494                 if (flags & EXT3_IMMUTABLE_FL)
1495                         inode->i_flags |= S_IMMUTABLE;
1496                 else
1497                         inode->i_flags &= ~S_IMMUTABLE;
1498                 if (flags & EXT3_NOATIME_FL)
1499                         inode->i_flags |= S_NOATIME;
1500                 else
1501                         inode->i_flags &= ~S_NOATIME;
1502
1503                 RETURN(0);
1504         }
1505         default:
1506                 RETURN(-ENOSYS);
1507         }
1508
1509         RETURN(0);
1510 }
1511
1512 /* this is only called in the case of forced umount. */
1513 void ll_umount_begin(struct super_block *sb)
1514 {
1515         struct ll_sb_info *sbi = ll_s2sbi(sb);
1516         struct obd_device *obd;
1517         struct obd_ioctl_data ioc_data = { 0 };
1518         ENTRY;
1519      
1520         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1521                sb->s_count, atomic_read(&sb->s_active));
1522         
1523         obd = class_exp2obd(sbi->ll_lmv_exp);
1524         if (obd == NULL) {
1525                 CERROR("Invalid MDC connection handle "LPX64"\n",
1526                        sbi->ll_lmv_exp->exp_handle.h_cookie);
1527                 EXIT;
1528                 return;
1529         }
1530         obd->obd_no_recov = 1;
1531         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lmv_exp,
1532                       sizeof(ioc_data), &ioc_data, NULL);
1533
1534         obd = class_exp2obd(sbi->ll_lov_exp);
1535         if (obd == NULL) {
1536                 CERROR("Invalid LOV connection handle "LPX64"\n",
1537                        sbi->ll_lov_exp->exp_handle.h_cookie);
1538                 EXIT;
1539                 return;
1540         }
1541
1542         obd->obd_no_recov = 1;
1543         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_lov_exp,
1544                       sizeof(ioc_data), &ioc_data, NULL);
1545
1546         /*
1547          * really, we'd like to wait until there are no requests outstanding,
1548          * and then continue.  For now, we just invalidate the requests,
1549          * schedule, and hope.
1550          */
1551         schedule();
1552
1553         EXIT;
1554 }
1555
1556 int ll_prep_inode(struct obd_export *lov_exp, struct obd_export *lmv_exp,
1557                   struct inode **inode, struct ptlrpc_request *req,
1558                   int offset, struct super_block *sb)
1559 {
1560         struct lustre_md md;
1561         int rc = 0;
1562
1563         rc = mdc_req2lustre_md(lmv_exp, req, offset, lov_exp, &md);
1564         if (rc)
1565                 RETURN(rc);
1566
1567         if (*inode) {
1568                 ll_update_inode(*inode, &md);
1569         } else {
1570                 LASSERT(sb);
1571                 *inode = ll_iget(sb, id_ino(&md.body->id1), &md);
1572                 if (*inode == NULL || is_bad_inode(*inode)) {
1573                         /* free the lsm if we allocated one above */
1574                         if (md.lsm != NULL)
1575                                 obd_free_memmd(lov_exp, &md.lsm);
1576                         if (md.mea != NULL)
1577                                 obd_free_memmd(lmv_exp,
1578                                                (struct lov_stripe_md**)&md.mea);
1579                         rc = -ENOMEM;
1580                         CERROR("new_inode -fatal: rc %d\n", rc);
1581                 }
1582         }
1583
1584         RETURN(rc);
1585 }
1586
1587 int ll_get_fid(struct obd_export *exp, struct lustre_id *idp,
1588                char *filename, struct lustre_id *ret)
1589 {
1590         struct ptlrpc_request *request = NULL;
1591         struct mds_body *body;
1592         __u64 valid = 0;
1593         int rc;
1594
1595         valid |= OBD_MD_FID;
1596         
1597         rc = md_getattr_lock(exp, idp, filename, strlen(filename) + 1,
1598                              valid, 0, &request);
1599         if (rc < 0) {
1600                 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
1601                        filename, rc);
1602                 return rc;
1603         }
1604
1605         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1606         LASSERT(body != NULL);
1607         LASSERT_REPSWABBED(request, 0);
1608
1609         *ret = body->id1;
1610         ptlrpc_req_finished(request);
1611
1612         return rc;
1613 }