Whamcloud - gitweb
1)add clonefs read in lustre
[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 }
445
446 int ll_fill_super(struct super_block *sb, void *data, int silent)
447 {
448         struct ll_sb_info *sbi;
449         char *osc = NULL;
450         char *mdc = NULL;
451         char *clone_opts = NULL;
452         int err;
453         ENTRY;
454
455         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
456
457         sbi = lustre_init_sbi(sb);
458         if (!sbi)
459                 RETURN(-ENOMEM);
460
461         sbi->ll_flags |= LL_SBI_READAHEAD;
462         ll_options(data, &osc, &mdc, &sbi->ll_flags, &clone_opts);
463
464         if (!osc) {
465                 CERROR("no osc\n");
466                 GOTO(out, err = -EINVAL);
467         }
468
469         if (!mdc) {
470                 CERROR("no mdc\n");
471                 GOTO(out, err = -EINVAL);
472         }
473
474         err = lustre_common_fill_super(sb, mdc, osc);
475 #if CONFIG_SNAPFS        
476         if (clone_opts) {
477                 int clone_index = 0;
478                 char *endp;
479
480                 /*set clone info to the super block*/
481                 clone_index = simple_strtoul(clone_opts, &endp, 0);
482                 err = lustre_set_clone_info(sb, clone_index);                
483         }
484 #endif         
485 out:
486         if (err)
487                 lustre_free_sbi(sb);
488
489         if (mdc)
490                 OBD_FREE(mdc, strlen(mdc) + 1);
491         if (osc)
492                 OBD_FREE(osc, strlen(osc) + 1);
493         if (clone_opts)
494                 OBD_FREE(clone_opts, strlen(clone_opts) + 1);
495
496         RETURN(err);
497 } /* ll_read_super */
498
499 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
500                               struct config_llog_instance *cfg, int allow_recov)
501 {
502         struct lustre_cfg lcfg;
503         struct portals_cfg pcfg;
504         char * peer = "MDS_PEER_UUID";
505         struct obd_device *obd;
506         struct lustre_handle mdc_conn = {0, };
507         struct obd_export *exp;
508         char * name = "mdc_dev";
509         class_uuid_t uuid;
510         struct obd_uuid mdc_uuid;
511         struct llog_ctxt *ctxt;
512         int rc = 0;
513         int err;
514         ENTRY;
515
516         if (lmd_bad_magic(lmd))
517                 RETURN(-EINVAL);
518
519         generate_random_uuid(uuid);
520         class_uuid_unparse(uuid, &mdc_uuid);
521
522         if (lmd->lmd_local_nid) {
523                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
524                 pcfg.pcfg_nal = lmd->lmd_nal;
525                 pcfg.pcfg_nid = lmd->lmd_local_nid;
526                 err = libcfs_nal_cmd(&pcfg);
527                 if (err <0)
528                         GOTO(out, err);
529         }
530
531         if (lmd->lmd_nal == SOCKNAL) {
532                 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
533                 pcfg.pcfg_nal     = lmd->lmd_nal;
534                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
535                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
536                 pcfg.pcfg_misc    = lmd->lmd_port;
537                 pcfg.pcfg_size    = 8388608;
538                 pcfg.pcfg_flags   = 0x4; /*share*/
539                 err = libcfs_nal_cmd(&pcfg);
540                 if (err <0)
541                         GOTO(out, err);
542         }
543
544         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
545         lcfg.lcfg_nid = lmd->lmd_server_nid;
546         lcfg.lcfg_inllen1 = strlen(peer) + 1;
547         lcfg.lcfg_inlbuf1 = peer;
548         lcfg.lcfg_nal = lmd->lmd_nal;
549         err = class_process_config(&lcfg);
550         if (err < 0)
551                 GOTO(out_del_conn, err);
552
553         LCFG_INIT(lcfg, LCFG_ATTACH, name);
554         lcfg.lcfg_inlbuf1 = "mdc";
555         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
556         lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
557         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
558         err = class_process_config(&lcfg);
559         if (err < 0)
560                 GOTO(out_del_uuid, err);
561
562         LCFG_INIT(lcfg, LCFG_SETUP, name);
563         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
564         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
565         lcfg.lcfg_inlbuf2 = peer;
566         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
567         err = class_process_config(&lcfg);
568         if (err < 0)
569                 GOTO(out_detach, err);
570
571         obd = class_name2obd(name);
572         if (obd == NULL)
573                 GOTO(out_cleanup, err = -EINVAL);
574
575         /* Disable initial recovery on this import */
576         err = obd_set_info(obd->obd_self_export,
577                            strlen("initial_recov"), "initial_recov",
578                            sizeof(allow_recov), &allow_recov);
579         if (err)
580                 GOTO(out_cleanup, err);
581
582         err = obd_connect(&mdc_conn, obd, &mdc_uuid);
583         if (err) {
584                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
585                 GOTO(out_cleanup, err);
586         }
587
588         exp = class_conn2export(&mdc_conn);
589
590         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
591         rc = class_config_process_llog(ctxt, profile, cfg);
592         if (rc)
593                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
594
595         err = obd_disconnect(exp, 0);
596
597 out_cleanup:
598         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
599         err = class_process_config(&lcfg);
600         if (err < 0)
601                 GOTO(out, err);
602
603 out_detach:
604         LCFG_INIT(lcfg, LCFG_DETACH, name);
605         err = class_process_config(&lcfg);
606         if (err < 0)
607                 GOTO(out, err);
608
609 out_del_uuid:
610         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
611         lcfg.lcfg_inllen1 = strlen(peer) + 1;
612         lcfg.lcfg_inlbuf1 = peer;
613         err = class_process_config(&lcfg);
614
615 out_del_conn:
616         if (lmd->lmd_nal == SOCKNAL) {
617                 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
618                 pcfg.pcfg_nal     = lmd->lmd_nal;
619                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
620                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
621                 pcfg.pcfg_flags   = 1; /*share*/
622                 err = libcfs_nal_cmd(&pcfg);
623                 if (err <0)
624                         GOTO(out, err);
625         }
626 out:
627         if (rc == 0)
628                 rc = err;
629
630         RETURN(rc);
631 }
632
633 int lustre_fill_super(struct super_block *sb, void *data, int silent)
634 {
635         struct lustre_mount_data * lmd = data;
636         struct ll_sb_info *sbi;
637         char *osc = NULL;
638         char *mdc = NULL;
639         int err;
640         ENTRY;
641
642         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
643         if (lmd_bad_magic(lmd))
644                 RETURN(-EINVAL);
645
646         sbi = lustre_init_sbi(sb);
647         if (!sbi)
648                 RETURN(-ENOMEM);
649
650         sbi->ll_flags |= LL_SBI_READAHEAD;
651
652         if (lmd->lmd_profile) {
653                 struct lustre_profile *lprof;
654                 struct config_llog_instance cfg;
655                 int len;
656
657                 if (lmd->lmd_mds[0] == '\0') {
658                         CERROR("no mds name\n");
659                         GOTO(out_free, err = -EINVAL);
660                 }
661
662                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
663                 if (sbi->ll_lmd == NULL)
664                         GOTO(out_free, err = -ENOMEM);
665                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
666
667                 /* generate a string unique to this super, let's try
668                  the address of the super itself.*/
669                 len = (sizeof(sb) * 2) + 1;
670                 OBD_ALLOC(sbi->ll_instance, len);
671                 if (sbi->ll_instance == NULL)
672                         GOTO(out_free, err = -ENOMEM);
673                 sprintf(sbi->ll_instance, "%p", sb);
674
675                 cfg.cfg_instance = sbi->ll_instance;
676                 cfg.cfg_uuid = sbi->ll_sb_uuid;
677                 cfg.cfg_local_nid = lmd->lmd_local_nid;
678                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
679                 if (err < 0) {
680                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
681                         GOTO(out_free, err);
682                 }
683
684                 lprof = class_get_profile(lmd->lmd_profile);
685                 if (lprof == NULL) {
686                         CERROR("No profile found: %s\n", lmd->lmd_profile);
687                         GOTO(out_free, err = -EINVAL);
688                 }
689                 if (osc)
690                         OBD_FREE(osc, strlen(osc) + 1);
691                 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
692                           strlen(sbi->ll_instance) + 2);
693                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
694
695                 if (mdc)
696                         OBD_FREE(mdc, strlen(mdc) + 1);
697                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
698                           strlen(sbi->ll_instance) + 2);
699                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
700         }
701
702         if (!osc) {
703                 CERROR("no osc\n");
704                 GOTO(out_free, err = -EINVAL);
705         }
706
707         if (!mdc) {
708                 CERROR("no mdc\n");
709                 GOTO(out_free, err = -EINVAL);
710         }
711
712         err = lustre_common_fill_super(sb, mdc, osc);
713
714         if (err)
715                 GOTO(out_free, err);
716
717 #if CONFIG_SNAPFS
718         if (lmd->lmd_clone_index) {
719                 err = lustre_set_clone_info(sb, lmd->lmd_clone_index);                
720         }
721         
722         if (err)
723                 GOTO(out_free, err);
724 #endif
725         
726 out_dev:
727         if (mdc)
728                 OBD_FREE(mdc, strlen(mdc) + 1);
729         if (osc)
730                 OBD_FREE(osc, strlen(osc) + 1);
731
732         RETURN(err);
733
734 out_free:
735         if (sbi->ll_lmd) {
736                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
737                 int err;
738
739                 if (sbi->ll_instance != NULL) {
740                         struct lustre_mount_data *lmd = sbi->ll_lmd;
741                         char * cln_prof;
742                         struct config_llog_instance cfg;
743
744                         cfg.cfg_instance = sbi->ll_instance;
745                         cfg.cfg_uuid = sbi->ll_sb_uuid;
746
747                         OBD_ALLOC(cln_prof, len);
748                         sprintf(cln_prof, "%s-clean", lmd->lmd_profile);
749
750                         err = lustre_process_log(lmd, cln_prof, &cfg, 0);
751                         if (err < 0)
752                                 CERROR("Unable to process log: %s\n", cln_prof);
753                         OBD_FREE(cln_prof, len);
754                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
755                 }
756                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
757         }
758 #if CONFIG_SNAPFS
759         lustre_cleanup_clone_info(sb);
760 #endif
761         lustre_free_sbi(sb);
762
763         goto out_dev;
764 } /* lustre_fill_super */
765
766 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
767 {
768         struct lustre_cfg lcfg;
769         struct obd_device *obd;
770         int next = 0;
771
772         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
773         {
774                 int err;
775
776                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
777                 err = class_process_config(&lcfg);
778                 if (err) {
779                         CERROR("cleanup failed: %s\n", obd->obd_name);
780                         //continue;
781                 }
782
783                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
784                 err = class_process_config(&lcfg);
785                 if (err) {
786                         CERROR("detach failed: %s\n", obd->obd_name);
787                         //continue;
788                 }
789         }
790
791         if (sbi->ll_lmd != NULL)
792                 class_del_profile(sbi->ll_lmd->lmd_profile);
793 }
794
795 void lustre_put_super(struct super_block *sb)
796 {
797         struct obd_device *obd;
798         struct ll_sb_info *sbi = ll_s2sbi(sb);
799         int force_umount = 0;
800         ENTRY;
801
802         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
803         obd = class_exp2obd(sbi->ll_mdc_exp);
804         if (obd)
805                 force_umount = obd->obd_no_recov;
806         obd = NULL;
807
808 #if CONFIG_SNAPFS
809         lustre_cleanup_clone_info(sb);
810 #endif
811
812         lustre_common_put_super(sb);
813         if (sbi->ll_lmd != NULL) {
814                 char * cln_prof;
815                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
816                 int err;
817                 struct config_llog_instance cfg;
818
819                 if (force_umount) {
820                         CERROR("force umount, doing manual cleanup\n");
821                         lustre_manual_cleanup(sbi);
822                         GOTO(free_lmd, 0);
823                 }
824
825                 cfg.cfg_instance = sbi->ll_instance;
826                 cfg.cfg_uuid = sbi->ll_sb_uuid;
827
828                 OBD_ALLOC(cln_prof, len);
829                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
830
831                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
832                 if (err < 0) {
833                         CERROR("Unable to process log: %s, doing manual cleanup"
834                                "\n", cln_prof);
835                         lustre_manual_cleanup(sbi);
836                 }
837
838                 OBD_FREE(cln_prof, len);
839         free_lmd:
840                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
841                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
842         }
843
844         lustre_free_sbi(sb);
845
846         EXIT;
847 } /* lustre_put_super */
848
849 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
850 {
851         struct obd_export *mdc_exp = sbi->ll_mdc_exp;
852         struct lustre_mount_data *lmd = sbi->ll_lmd;
853         struct llog_ctxt *ctxt;
854         struct config_llog_instance cfg;
855         char *profile = lmd->lmd_profile, *name = NULL;
856         int rc, namelen =  0, version;
857         ENTRY;
858
859         if (profile == NULL)
860                 RETURN(0);
861         if (lmd == NULL) {
862                 CERROR("Client not mounted with zero-conf; cannot process "
863                        "update log.\n");
864                 RETURN(0);
865         }
866
867         rc = ldlm_cli_cancel_unused(mdc_exp->exp_obd->obd_namespace, NULL,
868                                     LDLM_FL_CONFIG_CHANGE, NULL);
869         if (rc != 0)
870                 CWARN("ldlm_cli_cancel_unused(mdc): %d\n", rc);
871
872         rc = obd_cancel_unused(sbi->ll_osc_exp, NULL, LDLM_FL_CONFIG_CHANGE,
873                                NULL);
874         if (rc != 0)
875                 CWARN("obd_cancel_unused(lov): %d\n", rc);
876
877         cfg.cfg_instance = sbi->ll_instance;
878         cfg.cfg_uuid = sbi->ll_sb_uuid;
879         cfg.cfg_local_nid = lmd->lmd_local_nid;
880
881         namelen = strlen(profile) + 20; /* -clean-######### */
882         OBD_ALLOC(name, namelen);
883         if (name == NULL)
884                 RETURN(-ENOMEM);
885
886         if (clean) {
887                 version = sbi->ll_config_version - 1;
888                 sprintf(name, "%s-clean-%d", profile, version);
889         } else {
890                 version = sbi->ll_config_version + 1;
891                 sprintf(name, "%s-%d", profile, version);
892         }
893
894         CWARN("Applying configuration log %s\n", name);
895
896         ctxt = llog_get_context(&mdc_exp->exp_obd->obd_llogs,
897                                 LLOG_CONFIG_REPL_CTXT);
898         rc = class_config_process_llog(ctxt, name, &cfg);
899         if (rc == 0)
900                 sbi->ll_config_version = version;
901         CWARN("Finished applying configuration log %s: %d\n", name, rc);
902
903         if (rc == 0 && clean == 0) {
904                 struct lov_desc desc;
905                 int rc, valsize;
906                 valsize = sizeof(desc);
907                 rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1,
908                                   "lovdesc", &valsize, &desc);
909
910                 rc = obd_init_ea_size(mdc_exp,
911                                       obd_size_diskmd(sbi->ll_osc_exp, NULL),
912                                       (desc.ld_tgt_count *
913                                        sizeof(struct llog_cookie)));
914         }
915         OBD_FREE(name, namelen);
916         RETURN(rc);
917 }
918
919 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
920 {
921         struct inode *inode;
922         l_lock(&lock->l_resource->lr_namespace->ns_lock);
923         if (lock->l_ast_data)
924                 inode = igrab(lock->l_ast_data);
925         else
926                 inode = NULL;
927         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
928         return inode;
929 }
930
931 int null_if_equal(struct ldlm_lock *lock, void *data)
932 {
933         if (data == lock->l_ast_data)
934                 lock->l_ast_data = NULL;
935
936         if (lock->l_req_mode != lock->l_granted_mode)
937                 return LDLM_ITER_STOP;
938
939         return LDLM_ITER_CONTINUE;
940 }
941
942 void ll_clear_inode(struct inode *inode)
943 {
944         struct ll_fid fid;
945         struct ll_inode_info *lli = ll_i2info(inode);
946         struct ll_sb_info *sbi = ll_i2sbi(inode);
947         ENTRY;
948
949         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
950                inode->i_generation, inode);
951
952         ll_inode2fid(&fid, inode);
953         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
954         md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
955
956         if (lli->lli_smd)
957                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
958                                   null_if_equal, inode);
959
960         if (lli->lli_smd) {
961                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
962                 lli->lli_smd = NULL;
963         }
964
965         if (lli->lli_mea) {
966                 /* FIXME: change cbdata for mea here */
967                 obd_free_memmd(sbi->ll_mdc_exp,
968                                (struct lov_stripe_md **) &lli->lli_mea);
969                 lli->lli_mea = NULL;
970         }
971
972         if (lli->lli_symlink_name) {
973                 OBD_FREE(lli->lli_symlink_name,
974                          strlen(lli->lli_symlink_name) + 1);
975                 lli->lli_symlink_name = NULL;
976         }
977
978         EXIT;
979 }
980
981 /* If this inode has objects allocated to it (lsm != NULL), then the OST
982  * object(s) determine the file size and mtime.  Otherwise, the MDS will
983  * keep these values until such a time that objects are allocated for it.
984  * We do the MDS operations first, as it is checking permissions for us.
985  * We don't to the MDS RPC if there is nothing that we want to store there,
986  * otherwise there is no harm in updating mtime/atime on the MDS if we are
987  * going to do an RPC anyways.
988  *
989  * If we are doing a truncate, we will send the mtime and ctime updates
990  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
991  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
992  * at the same time.
993  */
994 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
995 {
996         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
997         struct ll_sb_info *sbi = ll_i2sbi(inode);
998         struct ptlrpc_request *request = NULL;
999         struct mdc_op_data op_data;
1000         int ia_valid = attr->ia_valid;
1001         int rc = 0;
1002         ENTRY;
1003
1004         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1005         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
1006
1007         if (ia_valid & ATTR_SIZE) {
1008                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1009                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1010                                attr->ia_size, ll_file_maxbytes(inode));
1011                         RETURN(-EFBIG);
1012                 }
1013
1014                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1015         }
1016
1017         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1018         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1019                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1020                         RETURN(-EPERM);
1021         }
1022
1023         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1024         if (attr->ia_valid & ATTR_CTIME) {
1025                 attr->ia_ctime = CURRENT_TIME;
1026                 attr->ia_valid |= ATTR_CTIME_SET;
1027         }
1028         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1029                 attr->ia_atime = CURRENT_TIME;
1030                 attr->ia_valid |= ATTR_ATIME_SET;
1031         }
1032         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1033                 attr->ia_mtime = CURRENT_TIME;
1034                 attr->ia_valid |= ATTR_MTIME_SET;
1035         }
1036
1037         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1038                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1039                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1040                        LTIME_S(CURRENT_TIME));
1041         if (lsm)
1042                 attr->ia_valid &= ~ATTR_SIZE;
1043
1044         /* If only OST attributes being set on objects, don't do MDS RPC.
1045          * In that case, we need to check permissions and update the local
1046          * inode ourselves so we can call obdo_from_inode() always. */
1047         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
1048                 struct lustre_md md;
1049                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1050
1051                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1052                                 attr, NULL, 0, NULL, 0, &request);
1053                 if (rc) {
1054                         ptlrpc_req_finished(request);
1055                         if (rc != -EPERM && rc != -EACCES)
1056                                 CERROR("md_setattr fails: rc = %d\n", rc);
1057                         RETURN(rc);
1058                 }
1059
1060                 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
1061                                        sbi->ll_osc_exp, &md);
1062                 if (rc) {
1063                         ptlrpc_req_finished(request);
1064                         RETURN(rc);
1065                 }
1066
1067                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
1068                  * but needed to set timestamps backwards on utime. */
1069                 inode_setattr(inode, attr);
1070                 ll_update_inode(inode, &md);
1071                 ptlrpc_req_finished(request);
1072
1073                 if (!lsm || !S_ISREG(inode->i_mode)) {
1074                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1075                         RETURN(0);
1076                 }
1077         } else {
1078                 /* The OST doesn't check permissions, but the alternative is
1079                  * a gratuitous RPC to the MDS.  We already rely on the client
1080                  * to do read/write/truncate permission checks, so is mtime OK?
1081                  */
1082                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1083                         /* from sys_utime() */
1084                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1085                                 if (current->fsuid != inode->i_uid &&
1086                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1087                                         RETURN(rc);
1088                         } else {
1089                                 /* from inode_change_ok() */
1090                                 if (current->fsuid != inode->i_uid &&
1091                                     !capable(CAP_FOWNER))
1092                                         RETURN(-EPERM);
1093                         }
1094                 }
1095
1096                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1097                 inode_setattr(inode, attr);
1098         }
1099
1100         /* We really need to get our PW lock before we change inode->i_size.
1101          * If we don't we can race with other i_size updaters on our node, like
1102          * ll_file_read.  We can also race with i_size propogation to other
1103          * nodes through dirtying and writeback of final cached pages.  This
1104          * last one is especially bad for racing o_append users on other
1105          * nodes. */
1106         if (ia_valid & ATTR_SIZE) {
1107                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1108                                                            OBD_OBJECT_EOF } };
1109                 struct lustre_handle lockh = { 0 };
1110                 int err, ast_flags = 0;
1111                 /* XXX when we fix the AST intents to pass the discard-range
1112                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1113                  * XXX here. */
1114                 if (attr->ia_size == 0)
1115                         ast_flags = LDLM_AST_DISCARD_DATA;
1116
1117                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
1118                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
1119                 up(&inode->i_sem);
1120                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1121                                     ast_flags);
1122                 down(&inode->i_sem);
1123                 if (rc != 0)
1124                         RETURN(rc);
1125
1126                 rc = vmtruncate(inode, attr->ia_size);
1127
1128                 /* We need to drop the semaphore here, because this unlock may
1129                  * result in a cancellation, which will need the i_sem */
1130                 up(&inode->i_sem);
1131                 /* unlock now as we don't mind others file lockers racing with
1132                  * the mds updates below? */
1133                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1134                 down(&inode->i_sem);
1135                 if (err) {
1136                         CERROR("ll_extent_unlock failed: %d\n", err);
1137                         if (!rc)
1138                                 rc = err;
1139                 }
1140         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1141                 struct obdo oa;
1142
1143                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1144                        inode->i_ino, LTIME_S(attr->ia_mtime));
1145                 oa.o_id = lsm->lsm_object_id;
1146                 oa.o_gr = lsm->lsm_object_gr;
1147                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1148                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1149                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1150                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
1151                 if (rc)
1152                         CERROR("obd_setattr fails: rc=%d\n", rc);
1153         }
1154         RETURN(rc);
1155 }
1156
1157 int ll_setattr(struct dentry *de, struct iattr *attr)
1158 {
1159         LBUG(); /* code is unused, but leave this in case of VFS changes */
1160         RETURN(-ENOSYS);
1161 }
1162
1163 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1164                        unsigned long max_age)
1165 {
1166         struct ll_sb_info *sbi = ll_s2sbi(sb);
1167         struct obd_statfs obd_osfs;
1168         int rc;
1169         ENTRY;
1170
1171         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1172         if (rc) {
1173                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1174                 RETURN(rc);
1175         }
1176
1177         osfs->os_type = sb->s_magic;
1178
1179         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1180                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1181
1182         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1183         if (rc) {
1184                 CERROR("obd_statfs fails: rc = %d\n", rc);
1185                 RETURN(rc);
1186         }
1187
1188         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1189                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1190                obd_osfs.os_files);
1191
1192         osfs->os_blocks = obd_osfs.os_blocks;
1193         osfs->os_bfree = obd_osfs.os_bfree;
1194         osfs->os_bavail = obd_osfs.os_bavail;
1195
1196         /* If we don't have as many objects free on the OST as inodes
1197          * on the MDS, we reduce the total number of inodes to
1198          * compensate, so that the "inodes in use" number is correct.
1199          */
1200         if (obd_osfs.os_ffree < osfs->os_ffree) {
1201                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1202                         obd_osfs.os_ffree;
1203                 osfs->os_ffree = obd_osfs.os_ffree;
1204         }
1205
1206         RETURN(rc);
1207 }
1208
1209 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1210 {
1211         struct obd_statfs osfs;
1212         int rc;
1213
1214         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1215         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1216
1217         /* For now we will always get up-to-date statfs values, but in the
1218          * future we may allow some amount of caching on the client (e.g.
1219          * from QOS or lprocfs updates). */
1220         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1221         if (rc)
1222                 return rc;
1223
1224         statfs_unpack(sfs, &osfs);
1225
1226         if (sizeof(sfs->f_blocks) == 4) {
1227                 while (osfs.os_blocks > ~0UL) {
1228                         sfs->f_bsize <<= 1;
1229
1230                         osfs.os_blocks >>= 1;
1231                         osfs.os_bfree >>= 1;
1232                         osfs.os_bavail >>= 1;
1233                 }
1234         }
1235
1236         sfs->f_blocks = osfs.os_blocks;
1237         sfs->f_bfree = osfs.os_bfree;
1238         sfs->f_bavail = osfs.os_bavail;
1239
1240         return 0;
1241 }
1242
1243 void dump_lsm(int level, struct lov_stripe_md *lsm)
1244 {
1245         CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1246                "stripe_size %u, stripe_count %u\n",
1247                lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1248                lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1249 }
1250
1251 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1252 {
1253         struct ll_inode_info *lli = ll_i2info(inode);
1254         struct mds_body *body = md->body;
1255         struct lov_stripe_md *lsm = md->lsm;
1256         struct mea *mea = md->mea;
1257         ENTRY;
1258
1259         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1260         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1261         if (lsm != NULL) {
1262                 LASSERT(lsm->lsm_object_gr > 0);
1263                 if (lli->lli_smd == NULL) {
1264                         lli->lli_smd = lsm;
1265                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1266                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1267                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1268                 } else {
1269                         int i;
1270                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1271                                 CERROR("lsm mismatch for inode %ld\n",
1272                                        inode->i_ino);
1273                                 CERROR("lli_smd:\n");
1274                                 dump_lsm(D_ERROR, lli->lli_smd);
1275                                 CERROR("lsm:\n");
1276                                 dump_lsm(D_ERROR, lsm);
1277                                 LBUG();
1278                         }
1279                         /* XXX FIXME -- We should decide on a safer (atomic) and
1280                          * more elegant way to update the lsm */
1281                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1282                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1283                                         lsm->lsm_oinfo[i].loi_id;
1284                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1285                                         lsm->lsm_oinfo[i].loi_gr;
1286                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1287                                         lsm->lsm_oinfo[i].loi_ost_idx;
1288                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1289                                         lsm->lsm_oinfo[i].loi_ost_gen;
1290                         }
1291                 }
1292                 /* bug 2844 - limit i_blksize for broken user-space apps */
1293                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1294                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1295                 if (lli->lli_smd != lsm)
1296                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1297         }
1298
1299         if (mea != NULL) {
1300                 if (lli->lli_mea == NULL) {
1301                         lli->lli_mea = mea;
1302                 } else {
1303                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1304                                 CERROR("mea mismatch for inode %lu\n",
1305                                         inode->i_ino);
1306                                 LBUG();
1307                         }
1308                 }
1309                 if (lli->lli_mea != mea)
1310                         obd_free_memmd(ll_i2mdcexp(inode),
1311                                         (struct lov_stripe_md **) &mea);
1312                         
1313         }
1314
1315         if (body->valid & OBD_MD_FLID)
1316                 inode->i_ino = body->ino;
1317         if (body->valid & OBD_MD_FLATIME)
1318                 LTIME_S(inode->i_atime) = body->atime;
1319         if (body->valid & OBD_MD_FLMTIME &&
1320             body->mtime > LTIME_S(inode->i_mtime)) {
1321                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1322                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1323                 LTIME_S(inode->i_mtime) = body->mtime;
1324         }
1325         if (body->valid & OBD_MD_FLCTIME &&
1326             body->ctime > LTIME_S(inode->i_ctime))
1327                 LTIME_S(inode->i_ctime) = body->ctime;
1328         if (body->valid & OBD_MD_FLMODE)
1329                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1330         if (body->valid & OBD_MD_FLTYPE)
1331                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1332         if (body->valid & OBD_MD_FLUID)
1333                 inode->i_uid = body->uid;
1334         if (body->valid & OBD_MD_FLGID)
1335                 inode->i_gid = body->gid;
1336         if (body->valid & OBD_MD_FLFLAGS)
1337                 inode->i_flags = body->flags;
1338         if (body->valid & OBD_MD_FLNLINK)
1339                 inode->i_nlink = body->nlink;
1340         if (body->valid & OBD_MD_FLGENER)
1341                 inode->i_generation = body->generation;
1342         if (body->valid & OBD_MD_FLRDEV)
1343 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1344                 inode->i_rdev = body->rdev;
1345 #else
1346                 inode->i_rdev = old_decode_dev(body->rdev);
1347 #endif
1348         if (body->valid & OBD_MD_FLSIZE)
1349                 inode->i_size = body->size;
1350         if (body->valid & OBD_MD_FLBLOCKS)
1351                 inode->i_blocks = body->blocks;
1352
1353         if (body->valid & OBD_MD_FLSIZE)
1354                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1355
1356         lli->lli_mds = body->mds;
1357         inode->i_dev = (kdev_t) body->mds;
1358         LASSERT(body->mds < 1000);
1359 }
1360
1361 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1362 static struct backing_dev_info ll_backing_dev_info = {
1363         .ra_pages       = 0,    /* No readahead */
1364         .memory_backed  = 0,    /* Does contribute to dirty memory */
1365 };
1366 #endif
1367
1368 void ll_read_inode2(struct inode *inode, void *opaque)
1369 {
1370         struct lustre_md *md = opaque;
1371         struct ll_inode_info *lli = ll_i2info(inode);
1372         ENTRY;
1373
1374         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1375                inode->i_generation, inode);
1376
1377         ll_lli_init(lli);
1378
1379         LASSERT(!lli->lli_smd);
1380
1381         /* Core attributes from the MDS first.  This is a new inode, and
1382          * the VFS doesn't zero times in the core inode so we have to do
1383          * it ourselves.  They will be overwritten by either MDS or OST
1384          * attributes - we just need to make sure they aren't newer. */
1385         LTIME_S(inode->i_mtime) = 0;
1386         LTIME_S(inode->i_atime) = 0;
1387         LTIME_S(inode->i_ctime) = 0;
1388
1389         inode->i_rdev = 0;
1390         ll_update_inode(inode, md);
1391
1392         /* OIDEBUG(inode); */
1393
1394         if (S_ISREG(inode->i_mode)) {
1395                 inode->i_op = &ll_file_inode_operations;
1396                 inode->i_fop = &ll_file_operations;
1397                 inode->i_mapping->a_ops = &ll_aops;
1398                 EXIT;
1399         } else if (S_ISDIR(inode->i_mode)) {
1400                 inode->i_op = &ll_dir_inode_operations;
1401                 inode->i_fop = &ll_dir_operations;
1402                 inode->i_mapping->a_ops = &ll_dir_aops;
1403                 EXIT;
1404         } else if (S_ISLNK(inode->i_mode)) {
1405                 inode->i_op = &ll_fast_symlink_inode_operations;
1406                 EXIT;
1407         } else {
1408                 inode->i_op = &ll_special_inode_operations;
1409
1410 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1411                 init_special_inode(inode, inode->i_mode,
1412                                    kdev_t_to_nr(inode->i_rdev));
1413
1414                 /* initializing backing dev info. */
1415                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1416 #else
1417                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1418 #endif
1419                 lli->ll_save_ifop = inode->i_fop;
1420
1421                 if (S_ISCHR(inode->i_mode))
1422                         inode->i_fop = &ll_special_chr_inode_fops;
1423                 else if (S_ISBLK(inode->i_mode))
1424                         inode->i_fop = &ll_special_blk_inode_fops;
1425                 else if (S_ISFIFO(inode->i_mode))
1426                         inode->i_fop = &ll_special_fifo_inode_fops;
1427                 else if (S_ISSOCK(inode->i_mode))
1428                         inode->i_fop = &ll_special_sock_inode_fops;
1429
1430                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1431                       inode->i_fop);
1432
1433                 if (lli->ll_save_ifop->owner) {
1434                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1435                               lli->ll_save_ifop->owner);
1436                 }
1437                 EXIT;
1438         }
1439 }
1440
1441 void ll_delete_inode(struct inode *inode)
1442 {
1443         int rc;
1444         struct ll_fid fid;
1445         struct ll_sb_info *sbi = ll_i2sbi(inode);
1446         ENTRY;
1447         
1448         ll_inode2fid(&fid, inode);
1449
1450         rc = md_delete_object(sbi->ll_mdc_exp, &fid);
1451         if (rc) {
1452                 CERROR("md_delete_object() failed, error %d.\n",
1453                        rc);
1454         }
1455
1456         clear_inode(inode);
1457         EXIT;
1458 }
1459
1460 int ll_iocontrol(struct inode *inode, struct file *file,
1461                  unsigned int cmd, unsigned long arg)
1462 {
1463         struct ll_sb_info *sbi = ll_i2sbi(inode);
1464         struct ptlrpc_request *req = NULL;
1465         int rc, flags = 0;
1466         ENTRY;
1467
1468         switch(cmd) {
1469         case EXT3_IOC_GETFLAGS: {
1470                 struct ll_fid fid;
1471                 unsigned long valid = OBD_MD_FLFLAGS;
1472                 struct mds_body *body;
1473
1474                 ll_inode2fid(&fid, inode);
1475                 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1476                 if (rc) {
1477                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1478                         RETURN(-abs(rc));
1479                 }
1480
1481                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1482
1483                 if (body->flags & S_APPEND)
1484                         flags |= EXT3_APPEND_FL;
1485                 if (body->flags & S_IMMUTABLE)
1486                         flags |= EXT3_IMMUTABLE_FL;
1487                 if (body->flags & S_NOATIME)
1488                         flags |= EXT3_NOATIME_FL;
1489
1490                 ptlrpc_req_finished (req);
1491
1492                 RETURN(put_user(flags, (int *)arg));
1493         }
1494         case EXT3_IOC_SETFLAGS: {
1495                 struct mdc_op_data op_data;
1496                 struct iattr attr;
1497                 struct obdo *oa;
1498                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1499
1500                 if (get_user(flags, (int *)arg))
1501                         RETURN(-EFAULT);
1502
1503                 oa = obdo_alloc();
1504                 if (!oa)
1505                         RETURN(-ENOMEM);
1506
1507                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1508
1509                 memset(&attr, 0x0, sizeof(attr));
1510                 attr.ia_attr_flags = flags;
1511                 attr.ia_valid |= ATTR_ATTR_FLAG;
1512
1513                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1514                                 &attr, NULL, 0, NULL, 0, &req);
1515                 if (rc) {
1516                         ptlrpc_req_finished(req);
1517                         if (rc != -EPERM && rc != -EACCES)
1518                                 CERROR("md_setattr fails: rc = %d\n", rc);
1519                         obdo_free(oa);
1520                         RETURN(rc);
1521                 }
1522                 ptlrpc_req_finished(req);
1523
1524                 oa->o_id = lsm->lsm_object_id;
1525                 oa->o_gr = lsm->lsm_object_gr;
1526                 oa->o_flags = flags;
1527                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1528
1529                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1530                 obdo_free(oa);
1531                 if (rc) {
1532                         if (rc != -EPERM && rc != -EACCES)
1533                                 CERROR("md_setattr fails: rc = %d\n", rc);
1534                         RETURN(rc);
1535                 }
1536
1537                 if (flags & EXT3_APPEND_FL)
1538                         inode->i_flags |= S_APPEND;
1539                 else
1540                         inode->i_flags &= ~S_APPEND;
1541                 if (flags & EXT3_IMMUTABLE_FL)
1542                         inode->i_flags |= S_IMMUTABLE;
1543                 else
1544                         inode->i_flags &= ~S_IMMUTABLE;
1545                 if (flags & EXT3_NOATIME_FL)
1546                         inode->i_flags |= S_NOATIME;
1547                 else
1548                         inode->i_flags &= ~S_NOATIME;
1549
1550                 RETURN(0);
1551         }
1552         default:
1553                 RETURN(-ENOSYS);
1554         }
1555
1556         RETURN(0);
1557 }
1558
1559 void ll_umount_begin(struct super_block *sb)
1560 {
1561         struct ll_sb_info *sbi = ll_s2sbi(sb);
1562         struct obd_device *obd;
1563         struct obd_ioctl_data ioc_data = { 0 };
1564         ENTRY;
1565         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1566
1567         obd = class_exp2obd(sbi->ll_mdc_exp);
1568         if (obd == NULL) {
1569                 CERROR("Invalid MDC connection handle "LPX64"\n",
1570                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1571                 EXIT;
1572                 return;
1573         }
1574         obd->obd_no_recov = 1;
1575         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1576                       &ioc_data, NULL);
1577
1578         obd = class_exp2obd(sbi->ll_osc_exp);
1579         if (obd == NULL) {
1580                 CERROR("Invalid LOV connection handle "LPX64"\n",
1581                        sbi->ll_osc_exp->exp_handle.h_cookie);
1582                 EXIT;
1583                 return;
1584         }
1585
1586         obd->obd_no_recov = 1;
1587         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1588                       &ioc_data, NULL);
1589
1590         /* Really, we'd like to wait until there are no requests outstanding,
1591          * and then continue.  For now, we just invalidate the requests,
1592          * schedule, and hope.
1593          */
1594         schedule();
1595
1596         EXIT;
1597 }
1598
1599 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1600                   struct inode **inode, struct ptlrpc_request *req,
1601                   int offset, struct super_block *sb)
1602 {
1603         struct lustre_md md;
1604         int rc = 0;
1605
1606         rc = mdc_req2lustre_md(mdc_exp, req, offset, osc_exp, &md);
1607         if (rc)
1608                 RETURN(rc);
1609
1610         if (*inode) {
1611                 ll_update_inode(*inode, &md);
1612         } else {
1613                 LASSERT(sb);
1614                 *inode = ll_iget(sb, md.body->ino, &md);
1615                 if (*inode == NULL || is_bad_inode(*inode)) {
1616                         /* free the lsm if we allocated one above */
1617                         if (md.lsm != NULL)
1618                                 obd_free_memmd(osc_exp, &md.lsm);
1619                         if (md.mea != NULL)
1620                                 obd_free_memmd(mdc_exp,
1621                                                (struct lov_stripe_md**)&md.mea);
1622                         rc = -ENOMEM;
1623                         CERROR("new_inode -fatal: rc %d\n", rc);
1624                 }
1625         }
1626
1627         RETURN(rc);
1628 }