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