Whamcloud - gitweb
bad3fb3d63878fc1809feb9e0741dbab77836b39
[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 #include <linux/seq_file.h>
31
32 #include <linux/lustre_lite.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_dlm.h>
35 #include <linux/lprocfs_status.h>
36 #include <linux/lustre_acl.h>
37 #include <linux/lustre_audit.h>
38 #include <linux/lustre_gs.h>
39 #include <linux/lustre_sec.h>
40 #include "llite_internal.h"
41
42 kmem_cache_t *ll_file_data_slab;
43 kmem_cache_t *ll_intent_slab;
44
45 extern struct address_space_operations ll_aops;
46 extern struct address_space_operations ll_dir_aops;
47
48 #ifndef log2
49 #define log2(n) ffz(~(n))
50 #endif
51
52 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
53 {
54         struct ll_sb_info *sbi = NULL;
55         class_uuid_t uuid;
56         ENTRY;
57
58         OBD_ALLOC(sbi, sizeof(*sbi));
59         if (!sbi)
60                 RETURN(NULL);
61
62         spin_lock_init(&sbi->ll_lock);
63         INIT_LIST_HEAD(&sbi->ll_pglist);
64         sbi->ll_pglist_gen = 0;
65         if (num_physpages < SBI_DEFAULT_RA_MAX / 4)
66                 sbi->ll_ra_info.ra_max_pages = num_physpages / 4;
67         else
68                 sbi->ll_ra_info.ra_max_pages = SBI_DEFAULT_RA_MAX;
69         INIT_LIST_HEAD(&sbi->ll_conn_chain);
70         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
71         INIT_LIST_HEAD(&sbi->ll_mnt_list);
72         
73         sema_init(&sbi->ll_gns_sem, 1);
74         spin_lock_init(&sbi->ll_gns_lock);
75         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
76         init_waitqueue_head(&sbi->ll_gns_waitq);
77         init_completion(&sbi->ll_gns_mount_finished);
78
79         /* this later may be reset via /proc/fs/... */
80         memcpy(sbi->ll_gns_oname, ".mntinfo", strlen(".mntinfo"));
81         sbi->ll_gns_oname[strlen(sbi->ll_gns_oname)] = '\0';
82         
83         /* this later may be reset via /proc/fs/... */
84         memcpy(sbi->ll_gns_upcall, "/usr/sbin/gns_upcall",
85                strlen("/usr/sbin/gns_upcall"));
86         sbi->ll_gns_upcall[strlen(sbi->ll_gns_upcall)] = '\0';
87
88         /* default values, may be changed via /proc/fs/... */
89         sbi->ll_gns_state = LL_GNS_IDLE;
90         sbi->ll_gns_pending_dentry = NULL;
91         atomic_set(&sbi->ll_gns_enabled, 1);
92         sbi->ll_gns_tick = GNS_TICK_TIMEOUT;
93         sbi->ll_gns_timeout = GNS_MOUNT_TIMEOUT;
94
95         sbi->ll_gns_timer.data = (unsigned long)sbi;
96         sbi->ll_gns_timer.function = ll_gns_timer_callback;
97         init_timer(&sbi->ll_gns_timer);
98         //audit mask
99         sbi->ll_audit_mask = AUDIT_OFF;
100         ll_set_sbi(sb, sbi);
101
102         generate_random_uuid(uuid);
103         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
104         RETURN(sbi);
105 }
106
107 void lustre_free_sbi(struct super_block *sb)
108 {
109         struct ll_sb_info *sbi = ll_s2sbi(sb);
110         ENTRY;
111
112         if (sbi != NULL) {
113                 list_del(&sbi->ll_gns_sbi_head);
114                 del_timer(&sbi->ll_gns_timer);
115                 OBD_FREE(sbi, sizeof(*sbi));
116         }
117         ll_set_sbi(sb, NULL);
118         EXIT;
119 }
120
121 int lustre_init_dt_desc(struct ll_sb_info *sbi)
122 {
123         __u32 valsize;
124         int rc = 0;
125         ENTRY;
126         
127         valsize = sizeof(sbi->ll_dt_desc);
128         memset(&sbi->ll_dt_desc, 0, sizeof(sbi->ll_dt_desc));
129         rc = obd_get_info(sbi->ll_dt_exp, strlen("lovdesc") + 1,
130                           "lovdesc", &valsize, &sbi->ll_dt_desc);
131         RETURN(rc);
132 }
133
134 static int lustre_connect_mds(struct super_block *sb, char *lmv,
135                               struct obd_connect_data *data,
136                               char *mds_security, int async, int pag)
137 {
138         struct ll_sb_info *sbi = ll_s2sbi(sb);
139         struct lustre_handle md_conn = {0, };
140         struct obd_device *md_obd;
141         struct obd_statfs osfs;
142         unsigned long sec_flags;
143         __u32 valsize;
144         int    err = 0;
145         ENTRY;       
146  
147         md_obd = class_name2obd(lmv);
148         if (!md_obd) {
149                 CERROR("MDC %s: not setup or attached\n", lmv);
150                 RETURN(-EINVAL);
151         }
152
153         obd_set_info(md_obd->obd_self_export, strlen("async"), "async",
154                      sizeof(async), &async);
155
156         if (mds_security == NULL)
157                 mds_security = "null";
158         
159         err = obd_set_info(md_obd->obd_self_export, strlen("sec"), "sec",
160                            strlen(mds_security), mds_security);
161         
162         if (err) {
163                 CERROR("LMV %s: failed to set security %s, err %d\n",
164                         lmv, mds_security, err);
165                 RETURN(err);
166         }
167
168         if (pag) {
169                 sec_flags = PTLRPC_SEC_FL_PAG;
170                 err = obd_set_info(md_obd->obd_self_export,
171                                    strlen("sec_flags"), "sec_flags",
172                                    sizeof(sec_flags), &sec_flags);
173                 if (err) {
174                         OBD_FREE(data, sizeof(*data));
175                         RETURN(err);
176                 }
177         }
178
179         err = obd_connect(&md_conn, md_obd, &sbi->ll_sb_uuid, data,
180                           OBD_OPT_REAL_CLIENT);
181         if (err == -EBUSY) {
182                 CERROR("An MDS (lmv %s) is performing recovery, of which this"
183                        " client is not a part.  Please wait for recovery to "
184                        "complete, abort, or time out.\n", lmv);
185                 GOTO(out, err);
186         } else if (err) {
187                 CERROR("cannot connect to %s: rc = %d\n", lmv, err);
188                 GOTO(out, err);
189         }
190
191         sbi->ll_md_exp = class_conn2export(&md_conn);
192        
193         err = obd_statfs(md_obd, &osfs, jiffies - HZ);
194         if (err)
195                 GOTO(out_disconnect, err);
196
197         if (!osfs.os_bsize) {
198                 CERROR("Invalid block size is detected.");
199                 GOTO(out_disconnect, err);
200         }
201
202         sb->s_magic = LL_SUPER_MAGIC;
203         sb->s_blocksize = osfs.os_bsize;
204         sb->s_blocksize_bits = log2(osfs.os_bsize);
205         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
206
207         /* in 2.6.x FS is not allowed to form s_dev */
208 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
209         {
210                 kdev_t devno;
211                 
212                 devno = get_uuid2int((char *)sbi->ll_md_exp->exp_obd->obd_uuid.uuid, 
213                                      strlen((char *)sbi->ll_md_exp->exp_obd->obd_uuid.uuid));
214                 
215                 sb->s_dev = devno;
216         }
217 #endif
218
219         /* after statfs, we are supposed to have connected to MDSs,
220          * so it's ok to check remote flag returned.
221          */
222         valsize = sizeof(&sbi->ll_remote);
223         err = obd_get_info(sbi->ll_md_exp, strlen("remote_flag"), "remote_flag",
224                            &valsize, &sbi->ll_remote);
225         if (err) {
226                 CERROR("fail to obtain remote flag\n");
227                 GOTO(out_disconnect, err);
228         }
229
230 out_disconnect:
231         if (err)
232                 obd_disconnect(sbi->ll_md_exp, 0);
233 out:
234         RETURN(err);
235 }
236
237 static int lustre_connect_ost(struct super_block *sb, char *lov, 
238                               struct obd_connect_data *data, 
239                               char *oss_security, int async, int pag)
240 {
241         struct ll_sb_info *sbi = ll_s2sbi(sb);
242         struct lustre_handle dt_conn = {0, };
243         struct obd_device *obd = NULL;
244         unsigned long sec_flags;
245         int err, mdsize; 
246
247         obd = class_name2obd(lov);
248         if (!obd) {
249                 CERROR("OSC %s: not setup or attached\n", lov);
250                 GOTO(out, err = -EINVAL);
251         }
252         obd_set_info(obd->obd_self_export, strlen("async"), "async",
253                      sizeof(async), &async);
254
255        if (oss_security == NULL)
256                 oss_security = "null";
257
258         err = obd_set_info(obd->obd_self_export, strlen("sec"), "sec",
259                            strlen(oss_security), oss_security);
260         if (err) {
261                 CERROR("LOV %s: failed to set security %s, err %d\n",
262                         lov, oss_security, err);
263                 RETURN(err);
264         }
265
266         /* FIXME Because of the async nature of file i/o, we never know
267          * who is actually dirty the pages; and any process have chance
268          * to trigger dirty-flushing within its own process context. So
269          * for simplicity we simply use root's credential, we suppose root
270          * always have credential.
271          */
272         if (pag)
273                 sec_flags = PTLRPC_SEC_FL_PAG;
274         else
275                 sec_flags = PTLRPC_SEC_FL_OSS;
276
277         err = obd_set_info(obd->obd_self_export,
278                            strlen("sec_flags"), "sec_flags",
279                            sizeof(sec_flags), &sec_flags);
280         if (err) {
281                 OBD_FREE(data, sizeof(*data));
282                 RETURN(err);
283         }
284
285         err = obd_connect(&dt_conn, obd, &sbi->ll_sb_uuid, data, 0);
286         if (err == -EBUSY) {
287                 CERROR("An OST (lov %s) is performing recovery, of which this"
288                        " client is not a part.  Please wait for recovery to "
289                        "complete, abort, or time out.\n", lov);
290                 GOTO(out, err);
291         } else if (err) {
292                 CERROR("cannot connect to %s: rc = %d\n", lov, err);
293                 GOTO(out, err);
294         }
295         sbi->ll_dt_exp = class_conn2export(&dt_conn);
296
297         err = lustre_init_dt_desc(sbi);
298        
299         if (err) {
300                 CWARN("init dt_desc error %d \n", err);
301                 GOTO(out, err = 0);
302         }
303         mdsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
304         obd_init_ea_size(sbi->ll_md_exp, mdsize, sbi->ll_dt_desc.ld_tgt_count *
305                          sizeof(struct llog_cookie));
306 out:
307         RETURN(err);
308 }
309
310 extern struct dentry_operations ll_d_ops;
311
312 static int lustre_init_root_inode(struct super_block *sb)
313 {
314         struct ll_sb_info *sbi = ll_s2sbi(sb);
315         struct ptlrpc_request *request = NULL;
316         struct inode *root = NULL;
317         struct lustre_md md;
318         int err = 0;
319         ENTRY;
320
321         err = md_getstatus(sbi->ll_md_exp, &sbi->ll_rootid);
322         if (err) {
323                 CERROR("cannot mds_connect: rc = %d\n", err);
324                 GOTO(out, err);
325         }
326         CDEBUG(D_SUPER, "rootid "DLID4"\n", OLID4(&sbi->ll_rootid));
327
328         sb->s_op = &lustre_super_operations;
329
330         /* make root inode */
331         err = md_getattr(sbi->ll_md_exp, &sbi->ll_rootid,
332                          (OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS | OBD_MD_FID),
333                          NULL, NULL, 0, 0, NULL, &request);
334         if (err) {
335                 CERROR("md_getattr failed for root: rc = %d\n", err);
336                 GOTO(out, err);
337         }
338
339         err = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
340                                 sbi->ll_dt_exp, &md);
341         if (err) {
342                 CERROR("failed to understand root inode md: rc = %d\n", err);
343                 ptlrpc_req_finished(request);
344                 GOTO(out, err);
345         }
346
347         LASSERT(id_ino(&sbi->ll_rootid) != 0);
348         root = ll_iget(sb, id_ino(&sbi->ll_rootid), &md);
349
350         ptlrpc_req_finished(request);
351
352         if (root == NULL || is_bad_inode(root)) {
353                 if (md.lsm != NULL)
354                     obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
355                 if (md.mea != NULL)
356                     obd_free_memmd(sbi->ll_md_exp,
357                                    (struct lov_stripe_md**)&md.mea);
358                 CERROR("lustre_lite: bad iget4 for root\n");
359                 GOTO(out_root, err = -EBADF);
360         }
361         sb->s_root = d_alloc_root(root);
362         sb->s_root->d_op = &ll_d_ops;
363 out_root:
364         if (err)
365                 iput(root);
366 out:
367         RETURN(err);
368 }
369
370 int lustre_common_fill_super(struct super_block *sb, char *lmv, char *lov,
371                              char *gkc, int async,  char *mds_security,  
372                              char *oss_security, __u32 *nllu, int pag, 
373                              __u64 *remote)
374 {
375         struct ll_sb_info *sbi = ll_s2sbi(sb);
376         struct obd_connect_data *data;
377         int err;
378         ENTRY;
379
380         /*process the connect flags*/
381         if ((*remote & (OBD_CONNECT_LOCAL | OBD_CONNECT_REMOTE)) ==
382                        (OBD_CONNECT_LOCAL | OBD_CONNECT_REMOTE)) {
383                 CERROR("wrong remote flag "LPX64"\n", *remote);
384                 RETURN(-EINVAL);
385         }
386
387         OBD_ALLOC(data, sizeof(*data));
388         if (!data)
389                 RETURN(-ENOMEM);
390
391         data->ocd_connect_flags |= *remote & (OBD_CONNECT_LOCAL |
392                                               OBD_CONNECT_REMOTE);
393         memcpy(data->ocd_nllu, nllu, sizeof(data->ocd_nllu));
394
395         if (proc_lustre_fs_root) {
396                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, 
397                                                   sb, lov, lmv);
398                 if (err < 0)
399                         CERROR("could not register mount in /proc/lustre");
400         }
401
402         /*connect mds */ 
403         err = lustre_connect_mds(sb, lmv, data, mds_security, async, pag);
404         if (err)
405                 GOTO(out, err);
406
407         /*connect OST*/
408         err = lustre_connect_ost(sb, lov, data, oss_security, async, pag);
409         if (err)
410                 GOTO(out_lmv, err);
411
412         err = lustre_init_crypto(sb, gkc, data, async);
413         if (err) {
414                 CERROR("Could not connect to GSS err %d\n", err);
415                 err = 0;
416         }
417         /*connect GSS*/
418         err = lustre_init_root_inode(sb);
419         if (err)
420                 GOTO(out_gks, err);
421
422         err = ll_close_thread_start(&sbi->ll_lcq);
423         if (err) {
424                 CERROR("cannot start close thread: rc %d\n", err);
425                 GOTO(out_root, err);
426         }
427
428         ll_gns_add_timer(sbi);
429
430         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
431            backing dev info assigned to inode mapping is used for
432            determining maximal readahead. */
433 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
434     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
435         /* bug 2805 - set VM readahead to zero */
436         vm_max_readahead = vm_min_readahead = 0;
437 #endif
438         sb->s_flags |= MS_POSIXACL;
439 #ifdef S_PDIROPS
440         CWARN("Enabling PDIROPS\n");
441         sb->s_flags |= S_PDIROPS;
442 #endif
443         if (data != NULL)
444                 OBD_FREE(data, sizeof(*data));
445         RETURN(err);
446 out_root:
447         if (sb->s_root)
448                 dput(sb->s_root);
449 out_gks:
450         lustre_destroy_crypto(sb);
451 out_lmv:
452         obd_disconnect(sbi->ll_md_exp, 0);
453 out:
454         if (data != NULL)
455                 OBD_FREE(data, sizeof(*data));
456         lprocfs_unregister_mountpoint(sbi);
457         RETURN(err);
458 }
459
460 void lustre_common_put_super(struct super_block *sb)
461 {
462         struct ll_sb_info *sbi = ll_s2sbi(sb);
463         struct hlist_node *tmp, *next;
464         ENTRY;
465
466         ll_gns_del_timer(sbi);
467         ll_close_thread_stop(sbi->ll_lcq);
468
469         list_del(&sbi->ll_conn_chain);
470         obd_disconnect(sbi->ll_dt_exp, 0);
471         
472         lustre_destroy_crypto(sb);
473
474         lprocfs_unregister_mountpoint(sbi);
475         if (sbi->ll_proc_root) {
476                 lprocfs_remove(sbi->ll_proc_root);
477                 sbi->ll_proc_root = NULL;
478         }
479
480         obd_disconnect(sbi->ll_md_exp, 0);
481
482         // We do this to get rid of orphaned dentries. That is not really trw.
483         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
484                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
485                 CWARN("orphan dentry %.*s (%p->%p) at unmount\n",
486                       dentry->d_name.len, dentry->d_name.name, dentry, next);
487                 shrink_dcache_parent(dentry);
488         }
489         EXIT;
490 }
491
492 char *ll_read_opt(const char *opt, char *data)
493 {
494         char *value;
495         char *retval;
496         ENTRY;
497
498         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
499         if (strncmp(opt, data, strlen(opt)))
500                 RETURN(NULL);
501         if ((value = strchr(data, '=')) == NULL)
502                 RETURN(NULL);
503
504         value++;
505         OBD_ALLOC(retval, strlen(value) + 1);
506         if (!retval) {
507                 CERROR("out of memory!\n");
508                 RETURN(NULL);
509         }
510
511         memcpy(retval, value, strlen(value)+1);
512         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
513         RETURN(retval);
514 }
515
516 int ll_set_opt(const char *opt, char *data, int fl)
517 {
518         ENTRY;
519
520         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
521         if (strncmp(opt, data, strlen(opt)))
522                 RETURN(0);
523         else
524                 RETURN(fl);
525 }
526
527 void ll_options(char *options, char **lov, char **lmv, char **gkc,
528                 char **mds_sec, char **oss_sec, int *async, int *flags)
529 {
530         char *this_char;
531 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
532         char *opt_ptr = options;
533 #endif
534         ENTRY;
535
536         if (!options) {
537                 EXIT;
538                 return;
539         }
540
541         *async = 0;
542 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
543         for (this_char = strtok (options, ",");
544              this_char != NULL;
545              this_char = strtok (NULL, ",")) {
546 #else
547         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
548 #endif
549                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
550                 if (!*lov && (*lov = ll_read_opt("osc", this_char)))
551                         continue;
552                 if (!*lmv && (*lmv = ll_read_opt("mdc", this_char)))
553                         continue;
554                 if (!*gkc && (*gkc = ll_read_opt("gkc", this_char)))
555                         continue;
556                 if (!strncmp(this_char, "lasync", strlen("lasync"))) {
557                         *async = 1;
558                         continue;
559                 }
560                 if (!*mds_sec && (*mds_sec = ll_read_opt("mds_sec", this_char)))
561                         continue;
562                 if (!*oss_sec && (*oss_sec = ll_read_opt("oss_sec", this_char)))
563                         continue;
564                 if (!(*flags & LL_SBI_NOLCK) &&
565                     ((*flags) = (*flags) |
566                                 ll_set_opt("nolock", this_char,
567                                            LL_SBI_NOLCK)))
568                         continue;
569         }
570         
571         EXIT;
572 }
573
574 void ll_lli_init(struct ll_inode_info *lli)
575 {
576         sema_init(&lli->lli_open_sem, 1);
577         sema_init(&lli->lli_size_sem, 1);
578         lli->lli_flags = 0;
579         lli->lli_size_pid = 0;
580         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
581         spin_lock_init(&lli->lli_lock);
582         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
583         INIT_LIST_HEAD(&lli->lli_close_item);
584         lli->lli_inode_magic = LLI_INODE_MAGIC;
585         memset(&lli->lli_id, 0, sizeof(lli->lli_id));
586         sema_init(&lli->lli_och_sem, 1);
587         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
588         lli->lli_mds_exec_och = NULL;
589         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
590         lli->lli_open_fd_exec_count = 0;
591         lli->lli_audit_mask = AUDIT_OFF;
592         lli->lli_key_info = NULL;
593         init_waitqueue_head(&lli->lli_dirty_wait);
594         lli->lli_io_epoch = 0;
595         INIT_LIST_HEAD(&lli->lli_capas);
596 }
597
598 int ll_fill_super(struct super_block *sb, void *data, int silent)
599 {
600         struct ll_sb_info *sbi;
601         char *lov = NULL, *lmv = NULL, *gkc = NULL;
602         char *mds_sec = NULL;
603         char *oss_sec = NULL;
604         int async, err;
605         __u32 nllu[2] = { NOBODY_UID, NOBODY_GID };
606         __u64 remote_flag = 0;    
607         ENTRY;
608
609         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
610
611         sbi = lustre_init_sbi(sb);
612         if (!sbi)
613                 RETURN(-ENOMEM);
614
615         sbi->ll_flags |= LL_SBI_READAHEAD;
616         ll_options(data, &lov, &lmv, &gkc, &mds_sec, &oss_sec,
617                    &async, &sbi->ll_flags);
618
619         if (!lov || !lmv) {
620                 CERROR("no osc %p or no mdc %p\n", lov, lmv);
621                 GOTO(out, err = -EINVAL);
622         }
623         
624         err = lustre_common_fill_super(sb, lmv, lov, gkc, async, mds_sec, 
625                                        oss_sec, nllu, 0, &remote_flag);
626         EXIT;
627 out:
628         if (err)
629                 lustre_free_sbi(sb);
630
631         if (lmv)
632                 OBD_FREE(lmv, strlen(lmv) + 1);
633         if (lov)
634                 OBD_FREE(lov, strlen(lov) + 1);
635         if (mds_sec)
636                 OBD_FREE(mds_sec, strlen(mds_sec) + 1);
637         if (oss_sec)
638                 OBD_FREE(oss_sec, strlen(oss_sec) + 1);
639         if (gkc)
640                 OBD_FREE(gkc, strlen(gkc) + 1);
641
642         return err;
643 } /* ll_read_super */
644
645 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
646                               struct config_llog_instance *cfg, int allow_recov)
647 {
648         struct lustre_cfg *lcfg = NULL;
649         struct lustre_cfg_bufs bufs;
650         struct portals_cfg pcfg;
651         char *peer = "MDS_PEER_UUID";
652         struct obd_device *obd;
653         struct lustre_handle md_conn = {0, };
654         struct obd_export *exp;
655         char *name = "mdc_dev";
656         class_uuid_t uuid;
657         struct obd_uuid lmv_uuid;
658         struct llog_ctxt *ctxt;
659         int rc = 0, err = 0;
660         ENTRY;
661
662         if (lmd_bad_magic(lmd))
663                 RETURN(-EINVAL);
664
665         generate_random_uuid(uuid);
666         class_uuid_unparse(uuid, &lmv_uuid);
667
668         if (lmd->lmd_local_nid) {
669                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
670                 pcfg.pcfg_nal = lmd->lmd_nal;
671                 pcfg.pcfg_nid = lmd->lmd_local_nid;
672                 rc = libcfs_nal_cmd(&pcfg);
673                 if (rc < 0)
674                         GOTO(out, rc);
675         }
676
677         if (lmd->lmd_nal == SOCKNAL ||
678             lmd->lmd_nal == OPENIBNAL ||
679             lmd->lmd_nal == IIBNAL ||
680             lmd->lmd_nal == VIBNAL ||
681             lmd->lmd_nal == RANAL) {
682                 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
683                 pcfg.pcfg_nal     = lmd->lmd_nal;
684                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
685                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
686                 pcfg.pcfg_misc    = lmd->lmd_port;
687                 rc = libcfs_nal_cmd(&pcfg);
688                 if (rc < 0)
689                         GOTO(out, rc);
690         }
691         lustre_cfg_bufs_reset(&bufs, name);
692         lustre_cfg_bufs_set_string(&bufs, 1, peer);
693
694         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
695         lcfg->lcfg_nal = lmd->lmd_nal;
696         lcfg->lcfg_nid = lmd->lmd_server_nid;
697         LASSERT(lcfg->lcfg_nal);
698         LASSERT(lcfg->lcfg_nid);
699         err = class_process_config(lcfg);
700         lustre_cfg_free(lcfg);
701         if (err < 0)
702                 GOTO(out_del_conn, err);
703
704         lustre_cfg_bufs_reset(&bufs, name);
705         lustre_cfg_bufs_set_string(&bufs, 1, OBD_MDC_DEVICENAME);
706         lustre_cfg_bufs_set_string(&bufs, 2, (char *)lmv_uuid.uuid);
707
708         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
709         err = class_process_config(lcfg);
710         lustre_cfg_free(lcfg);
711         if (err < 0)
712                 GOTO(out_del_uuid, err);
713
714         lustre_cfg_bufs_reset(&bufs, name);
715         lustre_cfg_bufs_set_string(&bufs, 1, lmd->lmd_mds);
716         lustre_cfg_bufs_set_string(&bufs, 2, peer);
717
718         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
719         err = class_process_config(lcfg);
720         lustre_cfg_free(lcfg);
721         if (err < 0)
722                 GOTO(out_detach, err);
723
724         obd = class_name2obd(name);
725         if (obd == NULL)
726                 GOTO(out_cleanup, rc = -EINVAL);
727
728         rc = obd_set_info(obd->obd_self_export, strlen("sec"), "sec",
729                           strlen(lmd->lmd_mds_security), lmd->lmd_mds_security);
730         if (rc)
731                 GOTO(out_cleanup, rc);
732
733         if (lmd->lmd_pag) {
734                 unsigned long sec_flags = PTLRPC_SEC_FL_PAG;
735                 rc = obd_set_info(obd->obd_self_export,
736                                   strlen("sec_flags"), "sec_flags",
737                                   sizeof(sec_flags), &sec_flags);
738                 if (rc)
739                         GOTO(out_cleanup, rc);
740         }
741
742         /* Disable initial recovery on this import */
743         rc = obd_set_info(obd->obd_self_export,
744                           strlen("initial_recov"), "initial_recov",
745                           sizeof(allow_recov), &allow_recov);
746         if (rc)
747                 GOTO(out_cleanup, rc);
748
749         rc = obd_connect(&md_conn, obd, &lmv_uuid, NULL, OBD_OPT_REAL_CLIENT);
750         if (rc) {
751                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, rc);
752                 GOTO(out_cleanup, rc);
753         }
754
755         exp = class_conn2export(&md_conn);
756
757         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
758         rc = class_config_process_llog(ctxt, profile, cfg);
759         if (rc)
760                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
761
762         err = obd_disconnect(exp, 0);
763         
764         EXIT;
765 out_cleanup:
766         lustre_cfg_bufs_reset(&bufs, name);
767         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
768         err = class_process_config(lcfg);
769         lustre_cfg_free(lcfg);
770         if (err < 0)
771                 GOTO(out, err);
772 out_detach:
773         lustre_cfg_bufs_reset(&bufs, name);
774         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
775         err = class_process_config(lcfg);
776         lustre_cfg_free(lcfg);
777         if (err < 0)
778                 GOTO(out, err);
779
780 out_del_uuid:
781         lustre_cfg_bufs_reset(&bufs, name);
782         lustre_cfg_bufs_set_string(&bufs, 1, peer);
783         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
784         err = class_process_config(lcfg);
785         lustre_cfg_free(lcfg);
786
787 out_del_conn:
788         if (lmd->lmd_nal == SOCKNAL ||
789             lmd->lmd_nal == OPENIBNAL ||
790             lmd->lmd_nal == IIBNAL ||
791             lmd->lmd_nal == VIBNAL ||
792             lmd->lmd_nal == RANAL) {
793                 int err2;
794
795                 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
796                 pcfg.pcfg_nal     = lmd->lmd_nal;
797                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
798                 pcfg.pcfg_flags   = 1;          /* single_share */
799                 err2 = libcfs_nal_cmd(&pcfg);
800                 if (err2 && !err)
801                         err = err2;
802                 if (err < 0)
803                         GOTO(out, err);
804         }
805 out:
806         if (rc == 0)
807                 rc = err;
808
809         return rc;
810 }
811
812 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
813 {
814         struct lustre_cfg *lcfg;
815         struct lustre_cfg_bufs bufs;
816         struct obd_device *obd;
817         int next = 0;
818
819         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL) {
820                 int err;
821
822                 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
823                 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
824                 err = class_process_config(lcfg);
825                 if (err) {
826                         CERROR("cleanup failed: %s\n", obd->obd_name);
827                         //continue;
828                 }
829                 
830                 lcfg->lcfg_command = LCFG_DETACH;
831                 err = class_process_config(lcfg);
832                 lustre_cfg_free(lcfg);
833                 if (err) {
834                         CERROR("detach failed: %s\n", obd->obd_name);
835                         //continue;
836                 }
837         }
838
839         if (sbi->ll_lmd != NULL)
840                 class_del_profile(sbi->ll_lmd->lmd_profile);
841 }
842
843 static int lustre_process_profile(struct super_block *sb, 
844                                   struct lustre_mount_data *lmd,
845                                   char **lov, char **lmv, char **gkc)  
846 {
847         struct ll_sb_info *sbi = ll_s2sbi(sb);
848         struct config_llog_instance cfg;
849         struct lustre_profile *lprof;
850         int len, err = 0;
851         ENTRY;
852
853         if (!lmd->lmd_profile)
854                 RETURN(0);
855  
856         if (lmd->lmd_mds[0] == '\0') {
857                 CERROR("no mds name\n");
858                 GOTO(out, err = -EINVAL);
859         }
860         lmd->lmd_mds_security[sizeof(lmd->lmd_mds_security) - 1] = 0;
861         lmd->lmd_oss_security[sizeof(lmd->lmd_oss_security) - 1] = 0;
862
863         OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
864         if (sbi->ll_lmd == NULL)
865                 GOTO(out, err = -ENOMEM);
866         memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
867
868         /* generate a string unique to this super, let's try the address of the
869          * super itself. */
870         len = (sizeof(sb) * 2) + 1;
871         OBD_ALLOC(sbi->ll_instance, len);
872         if (sbi->ll_instance == NULL)
873                 GOTO(out, err = -ENOMEM);
874         sprintf(sbi->ll_instance, "%p", sb);
875
876         cfg.cfg_instance = sbi->ll_instance;
877         cfg.cfg_uuid = sbi->ll_sb_uuid;
878         cfg.cfg_local_nid = lmd->lmd_local_nid;
879         err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
880         if (err < 0) {
881                 CERROR("Unable to process log: %s\n", lmd->lmd_profile);
882                 GOTO(out, err);
883         }
884
885         lprof = class_get_profile(lmd->lmd_profile);
886         if (lprof == NULL) {
887                 CERROR("No profile found: %s\n", lmd->lmd_profile);
888                 GOTO(out, err = -EINVAL);
889         }
890
891         OBD_ALLOC(*lov, strlen(lprof->lp_lov) +
892                   strlen(sbi->ll_instance) + 2);
893         if (*lov == NULL)
894                 GOTO(out, err = -ENOMEM);
895         
896         sprintf(*lov, "%s-%s", lprof->lp_lov, sbi->ll_instance);
897
898         OBD_ALLOC(*lmv, strlen(lprof->lp_lmv) +
899                   strlen(sbi->ll_instance) + 2);
900         if (*lmv == NULL)
901                 GOTO(out_free_lov, err = -ENOMEM);
902
903         sprintf(*lmv, "%s-%s", lprof->lp_lmv, sbi->ll_instance);
904
905         if (lprof->lp_gkc) {
906                OBD_ALLOC(*gkc, strlen(lprof->lp_gkc) +
907                          strlen(sbi->ll_instance) + 2);
908                if (*gkc == NULL)
909                        GOTO(out_free_lmv, err = -ENOMEM);
910                
911                sprintf(*gkc, "%s-%s", lprof->lp_gkc, sbi->ll_instance); 
912         }
913         
914         RETURN(err);
915 out_free_lmv:
916         OBD_FREE(*lmv, strlen(lprof->lp_lmv) +
917                  strlen(sbi->ll_instance) + 2);
918 out_free_lov:
919         OBD_FREE(*lov, strlen(lprof->lp_lov) +
920                  strlen(sbi->ll_instance) + 2);
921 out: 
922         return err; 
923 }
924
925 static int lustre_clean_profile(struct ll_sb_info *sbi, int force_umount)
926 {
927         struct lustre_mount_data *lmd = sbi->ll_lmd;
928         struct config_llog_instance cfg;
929         char *cl_prof;
930         int len, err = 0;
931         ENTRY; 
932  
933         if (!lmd)
934                 RETURN(err);
935
936         len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean") + 1;
937         
938         if (force_umount) {
939                 CERROR("force umount, doing manual cleanup\n");
940                 lustre_manual_cleanup(sbi);
941                 GOTO(free_lmd, 0);
942                 
943         }
944         if (sbi->ll_instance != NULL) {
945                 cfg.cfg_instance = sbi->ll_instance;
946                 cfg.cfg_uuid = sbi->ll_sb_uuid;
947
948                 OBD_ALLOC(cl_prof, len);
949                 if (!cl_prof) {
950                         CERROR("can't allocate memory, "
951                                "skipping processing cleanup profile.\n");
952                         GOTO(free_lmd, err = -ENOMEM);
953                 }
954                 
955                 sprintf(cl_prof, "%s-clean", lmd->lmd_profile);
956                 err = lustre_process_log(lmd, cl_prof, &cfg, 0);
957                 if (err < 0) {
958                         CERROR("Unable to process log: %s\n", cl_prof);
959                         lustre_manual_cleanup(sbi);
960                 }
961                 OBD_FREE(cl_prof, len);
962         }
963         EXIT;
964 free_lmd:
965         if (sbi->ll_instance)
966                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
967         OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
968         return err;
969 }
970
971 int lustre_fill_super(struct super_block *sb, void *data, int silent)
972 {
973         struct lustre_mount_data * lmd = data;
974         char *lov = NULL, *lmv = NULL, *gkc = NULL;
975         struct ll_sb_info *sbi;
976         int err;
977         ENTRY;
978
979         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
980         if (lmd_bad_magic(lmd))
981                 RETURN(-EINVAL);
982
983         sbi = lustre_init_sbi(sb);
984         if (!sbi)
985                 RETURN(-ENOMEM);
986
987         sbi->ll_flags |= LL_SBI_READAHEAD;
988
989         err = lustre_process_profile(sb, lmd, &lov, &lmv, &gkc);
990         if (err) {
991                 CERROR("Can not process the profile err %d \n", err);
992                 GOTO(out_free, err);
993         }
994         if (!lov || !lmv) {
995                 CERROR("no osc %p or no mdc %p \n", lov, lmv);
996                 GOTO(out_free, err = -EINVAL);
997         }
998
999         err = lustre_common_fill_super(sb, lmv, lov, gkc, lmd->lmd_async,
1000                                        lmd->lmd_mds_security,
1001                                        lmd->lmd_oss_security,
1002                                        &lmd->lmd_nllu, lmd->lmd_pag,
1003                                        &lmd->lmd_remote_flag);
1004
1005         if (err)
1006                 GOTO(out_free, err);
1007
1008         EXIT;
1009 out_dev:
1010         if (lmv)
1011                 OBD_FREE(lmv, strlen(lmv) + 1);
1012         if (lov)
1013                 OBD_FREE(lov, strlen(lov) + 1);
1014         if (gkc)
1015                 OBD_FREE(gkc, strlen(gkc) + 1);
1016         
1017         return err;
1018 out_free:
1019         lustre_clean_profile(sbi, 0);
1020         lustre_free_sbi(sb);
1021         goto out_dev;
1022
1023 } /* lustre_fill_super */
1024
1025 void lustre_put_super(struct super_block *sb)
1026 {
1027         struct obd_device *obd;
1028         struct ll_sb_info *sbi = ll_s2sbi(sb);
1029         int force_umount = 0;
1030         ENTRY;
1031
1032         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
1033         obd = class_exp2obd(sbi->ll_md_exp);
1034         if (obd)
1035                 force_umount = obd->obd_no_recov;
1036         obd = NULL;
1037
1038         lustre_common_put_super(sb);
1039         lustre_clean_profile(sbi, force_umount);
1040         lustre_free_sbi(sb);
1041
1042         EXIT;
1043 } /* lustre_put_super */
1044
1045 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
1046 {
1047         struct lustre_mount_data *lmd = sbi->ll_lmd;
1048         char *profile = lmd->lmd_profile, *name = NULL;
1049         struct config_llog_instance cfg;
1050         int rc, namelen =  0, version;
1051         struct llog_ctxt *ctxt;
1052         ENTRY;
1053
1054         if (profile == NULL)
1055                 RETURN(0);
1056         if (lmd == NULL) {
1057                 CERROR("Client not mounted with zero-conf; cannot "
1058                        "process update log.\n");
1059                 RETURN(0);
1060         }
1061
1062         cfg.cfg_instance = sbi->ll_instance;
1063         cfg.cfg_uuid = sbi->ll_sb_uuid;
1064         cfg.cfg_local_nid = lmd->lmd_local_nid;
1065
1066         namelen = strlen(profile) + 20; /* -clean-######### */
1067         OBD_ALLOC(name, namelen);
1068         if (name == NULL)
1069                 RETURN(-ENOMEM);
1070
1071         if (clean) {
1072                 version = sbi->ll_config_version - 1;
1073                 sprintf(name, "%s-clean-%d", profile, version);
1074         } else {
1075                 version = sbi->ll_config_version + 1;
1076                 sprintf(name, "%s-%d", profile, version);
1077         }
1078
1079         CWARN("Applying configuration log %s\n", name);
1080
1081         ctxt = llog_get_context(&sbi->ll_md_exp->exp_obd->obd_llogs,
1082                                 LLOG_CONFIG_REPL_CTXT);
1083         rc = class_config_process_llog(ctxt, name, &cfg);
1084         if (rc == 0)
1085                 sbi->ll_config_version = version;
1086         CWARN("Finished applying configuration log %s: %d\n", name, rc);
1087
1088         if (rc == 0 && clean == 0) {
1089                 struct lov_desc desc;
1090                 __u32 valsize;
1091                 int rc = 0;
1092                 
1093                 valsize = sizeof(desc);
1094                 rc = obd_get_info(sbi->ll_dt_exp, strlen("lovdesc") + 1,
1095                                   "lovdesc", &valsize, &desc);
1096
1097                 rc = obd_init_ea_size(sbi->ll_md_exp,
1098                                       obd_size_diskmd(sbi->ll_dt_exp, NULL),
1099                                       (desc.ld_tgt_count *
1100                                        sizeof(struct llog_cookie)));
1101         }
1102         OBD_FREE(name, namelen);
1103         RETURN(rc);
1104 }
1105
1106 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1107 {
1108         struct inode *inode = NULL;
1109
1110         /* NOTE: we depend on atomic igrab() -bzzz */
1111         lock_res_and_lock(lock);
1112         if (lock->l_ast_data) {
1113                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1114                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1115                         inode = igrab(lock->l_ast_data);
1116                 } else {
1117                         struct timeval now;
1118                         do_gettimeofday(&now);
1119                         inode = lock->l_ast_data;
1120                         LDLM_ERROR(lock, "granted at %lu.%lu, now %lu.%lu",
1121                                    lock->l_enqueued_time.tv_sec,
1122                                    lock->l_enqueued_time.tv_usec,
1123                                    now.tv_sec, now.tv_usec);
1124                         CDEBUG(inode->i_state & I_FREEING ? D_INFO : D_WARNING,
1125                                "l_ast_data %p is bogus: magic %0x8\n",
1126                                lock->l_ast_data, lli->lli_inode_magic);
1127                         CDEBUG(D_ERROR, "i_state = 0x%lx, l_ast_data %p is bogus: magic %0x8\n",
1128                                inode->i_state, lock->l_ast_data, lli->lli_inode_magic);
1129                         inode = NULL;
1130                         unlock_res_and_lock(lock);
1131                         LBUG();
1132                 }
1133         }
1134         unlock_res_and_lock(lock);
1135         return inode;
1136 }
1137
1138 int null_if_equal(struct ldlm_lock *lock, void *data)
1139 {
1140         if (data == lock->l_ast_data) {
1141                 lock->l_ast_data = NULL;
1142
1143                 if (lock->l_req_mode != lock->l_granted_mode)
1144                         LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
1145         }
1146
1147         return LDLM_ITER_CONTINUE;
1148 }
1149
1150 static void remote_acl_free(struct remote_acl *racl);
1151
1152 void ll_clear_inode(struct inode *inode)
1153 {
1154         struct lustre_id id;
1155         struct ll_inode_info *lli = ll_i2info(inode);
1156         struct ll_sb_info *sbi = ll_i2sbi(inode);
1157         struct obd_capa *ocapa, *tmp;
1158         ENTRY;
1159
1160         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1161                inode->i_generation, inode);
1162
1163         LASSERT(ll_is_inode_dirty(inode) == 0);
1164         ll_inode2id(&id, inode);
1165         
1166         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
1167         md_change_cbdata(sbi->ll_md_exp, &id, null_if_equal, inode);
1168
1169         LASSERT(!lli->lli_open_fd_write_count);
1170         LASSERT(!lli->lli_open_fd_read_count);
1171         LASSERT(!lli->lli_open_fd_exec_count);
1172         if (lli->lli_mds_write_och)
1173                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_WRITE);
1174         if (lli->lli_mds_exec_och)
1175                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_EXEC);
1176         if (lli->lli_mds_read_och)
1177                 ll_md_real_close(sbi->ll_md_exp, inode, FMODE_READ);
1178         if (lli->lli_smd)
1179                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
1180                                   null_if_equal, inode);
1181
1182         if (lli->lli_smd) {
1183                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
1184                 lli->lli_smd = NULL;
1185         }
1186
1187         if (lli->lli_mea) {
1188                 obd_free_memmd(sbi->ll_md_exp,
1189                                (struct lov_stripe_md **) &lli->lli_mea);
1190                 lli->lli_mea = NULL;
1191         }
1192
1193         LASSERT(sbi->ll_crypto_info != NULL);
1194         ll_crypto_destroy_inode_key(inode);
1195
1196         if (lli->lli_symlink_name) {
1197                 OBD_FREE(lli->lli_symlink_name,
1198                          strlen(lli->lli_symlink_name) + 1);
1199                 lli->lli_symlink_name = NULL;
1200         }
1201
1202         if (lli->lli_posix_acl) {
1203                 LASSERT(lli->lli_remote_acl == NULL);
1204                 posix_acl_release(lli->lli_posix_acl);
1205                 lli->lli_posix_acl = NULL;
1206         }
1207
1208         if (lli->lli_remote_acl) {
1209                 LASSERT(lli->lli_posix_acl == NULL);
1210                 remote_acl_free(lli->lli_remote_acl);
1211                 lli->lli_remote_acl = NULL;
1212         }
1213
1214         list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, u.client.lli_list)
1215                 capa_put(ocapa);
1216
1217         LASSERT(!mapping_has_pages(inode->i_mapping));
1218         lli->lli_inode_magic = LLI_INODE_DEAD;
1219         
1220         EXIT;
1221 }
1222
1223 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1224  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1225  * keep these values until such a time that objects are allocated for it.
1226  * We do the MDS operations first, as it is checking permissions for us.
1227  * We don't to the MDS RPC if there is nothing that we want to store there,
1228  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1229  * going to do an RPC anyways.
1230  *
1231  * If we are doing a truncate, we will send the mtime and ctime updates
1232  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1233  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1234  * at the same time.
1235  */
1236 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1237 {
1238         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1239         struct ll_inode_info *lli = ll_i2info(inode);
1240         struct ll_sb_info *sbi = ll_i2sbi(inode);
1241         struct ptlrpc_request *request = NULL;
1242         struct mdc_op_data *op_data;
1243         int ia_valid = attr->ia_valid;
1244         int err, rc = 0;
1245         ENTRY;
1246
1247         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1248         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
1249
1250         if (ia_valid & ATTR_SIZE) {
1251                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1252                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1253                                attr->ia_size, ll_file_maxbytes(inode));
1254                         RETURN(-EFBIG);
1255                 }
1256
1257                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1258         }
1259
1260         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1261         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1262                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1263                         RETURN(-EPERM);
1264         }
1265
1266         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1267         if (attr->ia_valid & ATTR_CTIME) {
1268                 attr->ia_ctime = CURRENT_TIME;
1269                 attr->ia_valid |= ATTR_CTIME_SET;
1270         }
1271         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1272                 attr->ia_atime = CURRENT_TIME;
1273                 attr->ia_valid |= ATTR_ATIME_SET;
1274         }
1275         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1276                 attr->ia_mtime = CURRENT_TIME;
1277                 attr->ia_valid |= ATTR_MTIME_SET;
1278         }
1279
1280         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1281                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1282                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1283                        LTIME_S(CURRENT_TIME));
1284
1285         /* If only OST attributes being set on objects, don't do MDS RPC.
1286          * In that case, we need to check permissions and update the local
1287          * inode ourselves so we can call obdo_from_inode() always. */
1288         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN /*| ATTR_RAW*/) : ~0)) {
1289                 struct lustre_md md;
1290                 void *key = NULL;
1291                 int  key_size = 0; 
1292
1293                 OBD_ALLOC(op_data, sizeof(*op_data));
1294                 if (op_data == NULL)
1295                         RETURN(-ENOMEM);
1296                 ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
1297
1298                 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
1299                         rc = ll_crypto_get_mac(inode, attr, NULL, 0, &key, 
1300                                                &key_size);
1301                         if (rc) {
1302                                 CERROR("can not get right mac, rc=%d\n", rc);
1303                                 if (key && key_size)
1304                                         OBD_FREE(key, key_size);
1305                                 RETURN(rc);
1306                         }
1307                 }
1308                 rc = md_setattr(sbi->ll_md_exp, op_data,
1309                                 attr, key, key_size, NULL, 0, NULL, 
1310                                 0, &request);
1311                 OBD_FREE(op_data, sizeof(*op_data));
1312                 
1313                 if (key && key_size) 
1314                         OBD_FREE(key, key_size);
1315                 if (rc) {
1316                         ptlrpc_req_finished(request);
1317                         if (rc != -EPERM && rc != -EACCES)
1318                                 CERROR("md_setattr fails: rc = %d\n", rc);
1319                         RETURN(rc);
1320                 }
1321                 rc = mdc_req2lustre_md(sbi->ll_md_exp, request, 0, 
1322                                        sbi->ll_dt_exp, &md);
1323                 if (rc) {
1324                         ptlrpc_req_finished(request);
1325                         RETURN(rc);
1326                 }
1327
1328                 if (attr->ia_valid & ATTR_SIZE) {
1329                         rc = ll_set_trunc_capa(request, 0, inode);
1330                         if (rc) {
1331                                 ptlrpc_req_finished(request);
1332                                 RETURN(rc);
1333                         }
1334                 }
1335
1336                 /* We call inode_setattr to adjust timestamps, but we first
1337                  * clear ATTR_SIZE to avoid invoking vmtruncate.
1338                  *
1339                  * NB: ATTR_SIZE will only be set at this point if the size
1340                  * resides on the MDS, ie, this file has no objects. */
1341                 attr->ia_valid &= ~ATTR_SIZE;
1342
1343                 /* 
1344                  * assigning inode_setattr() to @err to disable warning that
1345                  * function's result should be checked by by caller. error is
1346                  * impossible here, as vmtruncate() control path is disabled.
1347                  */
1348                 err = inode_setattr(inode, attr);
1349                 ll_update_inode(inode, &md);
1350                 ptlrpc_req_finished(request);
1351
1352                 if (!lsm || !S_ISREG(inode->i_mode)) {
1353                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1354                         RETURN(0);
1355                 }
1356         } else {
1357                 /* The OST doesn't check permissions, but the alternative is
1358                  * a gratuitous RPC to the MDS.  We already rely on the client
1359                  * to do read/write/truncate permission checks, so is mtime OK?
1360                  */
1361                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1362                         /* from sys_utime() */
1363                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1364                                 if (current->fsuid != inode->i_uid &&
1365                                     (rc = ll_permission(inode, MAY_WRITE, NULL)) != 0)
1366                                         RETURN(rc);
1367                         } else {
1368                                 /* from inode_change_ok() */
1369                                 if (current->fsuid != inode->i_uid &&
1370                                     !capable(CAP_FOWNER))
1371                                         RETURN(-EPERM);
1372                         }
1373                 }
1374
1375                 if (lsm)
1376                         attr->ia_valid &= ~ATTR_SIZE;
1377
1378                 /* won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1379                 err = inode_setattr(inode, attr);
1380                 /* 
1381                  * assigning inode_setattr() to @err to disable warning that
1382                  * function's result should be checked by by caller. error is
1383                  * impossible here, as vmtruncate() control path is disabled.
1384                  */
1385         }
1386
1387         /* We really need to get our PW lock before we change inode->i_size.
1388          * If we don't we can race with other i_size updaters on our node, like
1389          * ll_file_read.  We can also race with i_size propogation to other
1390          * nodes through dirtying and writeback of final cached pages.  This
1391          * last one is especially bad for racing o_append users on other
1392          * nodes. */
1393         if (ia_valid & ATTR_SIZE) {
1394                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1395                                                            OBD_OBJECT_EOF } };
1396                 struct lustre_handle lockh = { 0 };
1397                 int err, ast_flags = 0;
1398                 /* XXX when we fix the AST intents to pass the discard-range
1399                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1400                  * XXX here. */
1401                 if (attr->ia_size == 0)
1402                         ast_flags = LDLM_AST_DISCARD_DATA;
1403
1404                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1405                                     ast_flags, &ll_i2sbi(inode)->ll_seek_stime);
1406
1407                 if (rc != 0)
1408                         RETURN(rc);
1409
1410                 down(&lli->lli_size_sem);
1411                 lli->lli_size_pid = current->pid;
1412                 rc = vmtruncate(inode, attr->ia_size);
1413                 if (rc != 0) {
1414                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1415                         lli->lli_size_pid = 0;
1416                         up(&lli->lli_size_sem);
1417                 }
1418
1419                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1420                 if (err) {
1421                         CERROR("ll_extent_unlock failed: %d\n", err);
1422                         if (!rc)
1423                                 rc = err;
1424                 }
1425         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET | ATTR_UID | ATTR_GID)) {
1426                 struct obdo *oa = NULL;
1427
1428                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1429                        inode->i_ino, LTIME_S(attr->ia_mtime));
1430
1431                 oa = obdo_alloc();
1432                 if (oa == NULL)
1433                         RETURN(-ENOMEM);
1434
1435                 oa->o_id = lsm->lsm_object_id;
1436                 oa->o_gr = lsm->lsm_object_gr;
1437                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1438
1439                 /* adding uid and gid, needed for quota */
1440                 if (ia_valid & ATTR_UID) {
1441                         oa->o_uid = inode->i_uid;
1442                         oa->o_valid |= OBD_MD_FLUID;
1443                 }
1444
1445                 if (ia_valid & ATTR_GID) {
1446                         oa->o_gid = inode->i_gid;
1447                         oa->o_valid |= OBD_MD_FLGID;
1448                 }
1449
1450                 *(obdo_id(oa)) = lli->lli_id;
1451                 oa->o_valid |= OBD_MD_FLIFID;
1452
1453                 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1454                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1455                 rc = obd_setattr(sbi->ll_dt_exp, oa, lsm, NULL, NULL);
1456                 obdo_free(oa);
1457                 if (rc)
1458                         CERROR("obd_setattr fails: rc = %d\n", rc);
1459         }
1460
1461         RETURN(rc);
1462 }
1463
1464 int ll_setattr(struct dentry *de, struct iattr *attr)
1465 {
1466         LASSERT(de->d_inode);
1467         return ll_setattr_raw(de->d_inode, attr);
1468 }
1469
1470 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1471                        unsigned long max_age)
1472 {
1473         struct ll_sb_info *sbi = ll_s2sbi(sb);
1474         struct obd_statfs obd_osfs;
1475         int rc;
1476         ENTRY;
1477
1478         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1479         if (rc) {
1480                 CERROR("obd_statfs fails: rc = %d\n", rc);
1481                 RETURN(rc);
1482         }
1483
1484         osfs->os_type = sb->s_magic;
1485
1486         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1487                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1488
1489         rc = obd_statfs(class_exp2obd(sbi->ll_dt_exp), &obd_osfs, max_age);
1490         if (rc) {
1491                 CERROR("obd_statfs fails: rc = %d\n", rc);
1492                 RETURN(rc);
1493         }
1494
1495         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1496                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1497                obd_osfs.os_files);
1498
1499         osfs->os_blocks = obd_osfs.os_blocks;
1500         osfs->os_bfree = obd_osfs.os_bfree;
1501         osfs->os_bavail = obd_osfs.os_bavail;
1502
1503         /* If we don't have as many objects free on the OST as inodes
1504          * on the MDS, we reduce the total number of inodes to
1505          * compensate, so that the "inodes in use" number is correct.
1506          */
1507         if (obd_osfs.os_ffree < osfs->os_ffree) {
1508                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1509                         obd_osfs.os_ffree;
1510                 osfs->os_ffree = obd_osfs.os_ffree;
1511         }
1512
1513         RETURN(rc);
1514 }
1515
1516 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1517 {
1518         struct obd_statfs osfs;
1519         int rc;
1520
1521         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p\n", sb);
1522         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1523
1524         /* For now we will always get up-to-date statfs values, but in the
1525          * future we may allow some amount of caching on the client (e.g.
1526          * from QOS or lprocfs updates). */
1527         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1528         if (rc)
1529                 return rc;
1530
1531         statfs_unpack(sfs, &osfs);
1532
1533         if (sizeof(sfs->f_blocks) == 4) {
1534                 while (osfs.os_blocks > ~0UL) {
1535                         sfs->f_bsize <<= 1;
1536
1537                         osfs.os_blocks >>= 1;
1538                         osfs.os_bfree >>= 1;
1539                         osfs.os_bavail >>= 1;
1540                 }
1541         }
1542
1543         sfs->f_blocks = osfs.os_blocks;
1544         sfs->f_bfree = osfs.os_bfree;
1545         sfs->f_bavail = osfs.os_bavail;
1546
1547         return 0;
1548 }
1549
1550
1551 /********************************
1552  * remote acl                   *
1553  ********************************/
1554
1555 static struct remote_acl *remote_acl_alloc(void)
1556 {
1557         struct remote_acl *racl;
1558         int i;
1559
1560         OBD_ALLOC(racl, sizeof(*racl));
1561         if (!racl)
1562                 return NULL;
1563
1564         spin_lock_init(&racl->ra_lock);
1565         init_MUTEX(&racl->ra_update_sem);
1566
1567         for (i = 0; i < REMOTE_ACL_HASHSIZE; i++)
1568                 INIT_LIST_HEAD(&racl->ra_perm_cache[i]);
1569
1570         return racl;
1571 }
1572
1573 /*
1574  * caller should guarantee no race here.
1575  */
1576 static void remote_perm_flush_xperms(struct lustre_remote_perm *perm)
1577 {
1578         struct remote_perm_setxid *xperm;
1579
1580         while (!list_empty(&perm->lrp_setxid_perms)) {
1581                 xperm = list_entry(perm->lrp_setxid_perms.next,
1582                                    struct remote_perm_setxid,
1583                                    list);
1584                 list_del(&xperm->list);
1585                 OBD_FREE(xperm, sizeof(*xperm));
1586         }
1587 }
1588
1589 /*
1590  * caller should guarantee no race here.
1591  */
1592 static void remote_acl_flush(struct remote_acl *racl)
1593 {
1594         struct list_head *head;
1595         struct lustre_remote_perm *perm, *tmp;
1596         int i;
1597
1598         for (i = 0; i < REMOTE_ACL_HASHSIZE; i++) {
1599                 head = &racl->ra_perm_cache[i];
1600
1601                 list_for_each_entry_safe(perm, tmp, head, lrp_list) {
1602                         remote_perm_flush_xperms(perm);
1603                         list_del(&perm->lrp_list);
1604                         OBD_FREE(perm, sizeof(*perm));
1605                 }
1606         }
1607 }
1608
1609 static void remote_acl_free(struct remote_acl *racl)
1610 {
1611         if (!racl)
1612                 return;
1613
1614         down(&racl->ra_update_sem);
1615         spin_lock(&racl->ra_lock);
1616         remote_acl_flush(racl);
1617         spin_unlock(&racl->ra_lock);
1618         up(&racl->ra_update_sem);
1619
1620         OBD_FREE(racl, sizeof(*racl));
1621 }
1622
1623 static inline int remote_acl_hashfunc(__u32 id)
1624 {
1625         return (id & (REMOTE_ACL_HASHSIZE - 1));
1626 }
1627
1628 static
1629 int __remote_acl_check(struct remote_acl *racl, unsigned int *perm)
1630 {
1631         struct list_head *head;
1632         struct lustre_remote_perm *lperm;
1633         struct remote_perm_setxid *xperm;
1634         int found = 0, rc = -ENOENT;
1635
1636         LASSERT(racl);
1637         head = &racl->ra_perm_cache[remote_acl_hashfunc(current->uid)];
1638         spin_lock(&racl->ra_lock);
1639
1640         list_for_each_entry(lperm, head, lrp_list) {
1641                 if (lperm->lrp_auth_uid == current->uid) {
1642                         found = 1;
1643                         break;
1644                 }
1645         }
1646
1647         if (!found)
1648                 goto out;
1649
1650         if (lperm->lrp_auth_uid == current->fsuid &&
1651             lperm->lrp_auth_gid == current->fsgid) {
1652                 if (lperm->lrp_valid) {
1653                         *perm = lperm->lrp_perm;
1654                         rc = 0;
1655                 }
1656                 goto out;
1657         } else if ((!lperm->lrp_setuid &&
1658                     lperm->lrp_auth_uid != current->fsuid) ||
1659                    (!lperm->lrp_setgid &&
1660                     lperm->lrp_auth_gid != current->fsgid))  {
1661                 *perm = 0;
1662                 rc = 0;
1663                 goto out;
1664         }
1665
1666         list_for_each_entry(xperm, &lperm->lrp_setxid_perms, list) {
1667                 if (xperm->uid == current->fsuid &&
1668                     xperm->gid == current->fsgid) {
1669                         *perm = xperm->perm;
1670                         rc = 0;
1671                         goto out;
1672                 }
1673         }
1674
1675 out:
1676         spin_unlock(&racl->ra_lock);
1677         return rc;
1678 }
1679
1680 static
1681 int __remote_acl_update(struct remote_acl *racl,
1682                         struct mds_remote_perm *mperm,
1683                         struct lustre_remote_perm *lperm,
1684                         struct remote_perm_setxid *xperm)
1685 {
1686         struct list_head *head;
1687         struct lustre_remote_perm *lp;
1688         struct remote_perm_setxid *xp;
1689         int found = 0, setuid = 0, setgid = 0;
1690
1691         LASSERT(racl);
1692         LASSERT(mperm);
1693         LASSERT(lperm);
1694         LASSERT(current->uid == mperm->mrp_auth_uid);
1695
1696         if (current->fsuid != mperm->mrp_auth_uid)
1697                 setuid = 1;
1698         if (current->fsgid != mperm->mrp_auth_gid)
1699                 setgid = 1;
1700
1701         head = &racl->ra_perm_cache[remote_acl_hashfunc(current->uid)];
1702         spin_lock(&racl->ra_lock);
1703
1704         list_for_each_entry(lp, head, lrp_list) {
1705                 if (lp->lrp_auth_uid == current->uid) {
1706                         found = 1;
1707                         break;
1708                 }
1709         }
1710
1711         if (found) {
1712                 OBD_FREE(lperm, sizeof(*lperm));
1713
1714                 if (!lp->lrp_valid && !setuid && !setgid) {
1715                         lp->lrp_perm = mperm->mrp_perm;
1716                         lp->lrp_valid = 1;
1717                 }
1718
1719                 /* sanity check for changes of setxid rules */
1720                 if ((lp->lrp_setuid != 0) != (mperm->mrp_allow_setuid != 0)) {
1721                         CWARN("setuid changes: %d => %d\n",
1722                               (lp->lrp_setuid != 0),
1723                               (mperm->mrp_allow_setuid != 0));
1724                         lp->lrp_setuid = (mperm->mrp_allow_setuid != 0);
1725                 }
1726
1727                 if ((lp->lrp_setgid != 0) != (mperm->mrp_allow_setgid != 0)) {
1728                         CWARN("setgid changes: %d => %d\n",
1729                               (lp->lrp_setgid != 0),
1730                               (mperm->mrp_allow_setgid != 0));
1731                         lp->lrp_setgid = (mperm->mrp_allow_setgid != 0);
1732                 }
1733
1734                 if (!lp->lrp_setuid && !lp->lrp_setgid &&
1735                     !list_empty(&lp->lrp_setxid_perms)) {
1736                         remote_perm_flush_xperms(lp);
1737                 }
1738         } else {
1739                 /* initialize lperm and linked into hashtable
1740                  */
1741                 INIT_LIST_HEAD(&lperm->lrp_setxid_perms);
1742                 lperm->lrp_auth_uid = mperm->mrp_auth_uid;
1743                 lperm->lrp_auth_gid = mperm->mrp_auth_gid;
1744                 lperm->lrp_setuid = (mperm->mrp_allow_setuid != 0);
1745                 lperm->lrp_setgid = (mperm->mrp_allow_setgid != 0);
1746                 list_add(&lperm->lrp_list, head);
1747
1748                 if (!setuid && !setgid) {
1749                         /* in this case, i'm the authenticated user,
1750                          * and mrp_perm is for me.
1751                          */
1752                         lperm->lrp_perm = mperm->mrp_perm;
1753                         lperm->lrp_valid = 1;
1754                         spin_unlock(&racl->ra_lock);
1755
1756                         if (xperm)
1757                                 OBD_FREE(xperm, sizeof(*xperm));
1758                         return 0;
1759                 }
1760
1761                 lp = lperm;
1762                 /* fall through */
1763         }
1764
1765         LASSERT(lp->lrp_setuid || lp->lrp_setgid ||
1766                 list_empty(&lp->lrp_setxid_perms));
1767
1768         /* if no xperm supplied, we are all done here */
1769         if (!xperm) {
1770                 spin_unlock(&racl->ra_lock);
1771                 return 0;
1772         }
1773
1774         /* whether we allow setuid/setgid */
1775         if ((!lp->lrp_setuid && setuid) || (!lp->lrp_setgid && setgid)) {
1776                 OBD_FREE(xperm, sizeof(*xperm));
1777                 spin_unlock(&racl->ra_lock);
1778                 return 0;
1779         }
1780
1781         /* traverse xperm list */
1782         list_for_each_entry(xp, &lp->lrp_setxid_perms, list) {
1783                 if (xp->uid == current->fsuid &&
1784                     xp->gid == current->fsgid) {
1785                         if (xp->perm != mperm->mrp_perm) {
1786                                 /* actually this should not happen */
1787                                 CWARN("perm changed: %o => %o\n",
1788                                       xp->perm, mperm->mrp_perm);
1789                                 xp->perm = mperm->mrp_perm;
1790                         }
1791                         OBD_FREE(xperm, sizeof(*xperm));
1792                         spin_unlock(&racl->ra_lock);
1793                         return 0;
1794                 }
1795         }
1796
1797         /* finally insert this xperm */
1798         xperm->uid = current->fsuid;
1799         xperm->gid = current->fsgid;
1800         xperm->perm = mperm->mrp_perm;
1801         list_add(&xperm->list, &lp->lrp_setxid_perms);
1802
1803         spin_unlock(&racl->ra_lock);
1804         return 0;
1805 }
1806
1807 /*
1808  * remote_acl semaphore must be held by caller
1809  */
1810 static
1811 int remote_acl_update_locked(struct remote_acl *racl,
1812                              struct mds_remote_perm *mperm)
1813 {
1814         struct lustre_remote_perm *lperm;
1815         struct remote_perm_setxid *xperm;
1816         int setuid = 0, setgid = 0;
1817
1818         might_sleep();
1819
1820         if (current->uid != mperm->mrp_auth_uid) {
1821                 CERROR("current uid %u while authenticated as %u\n",
1822                        current->uid, mperm->mrp_auth_uid);
1823                 return -EINVAL;
1824         }
1825
1826         if (current->fsuid != mperm->mrp_auth_uid)
1827                 setuid = 1;
1828         if (current->fsgid == mperm->mrp_auth_gid)
1829                 setgid = 1;
1830
1831         OBD_ALLOC(lperm, sizeof(*lperm));
1832         if (!lperm)
1833                 return -ENOMEM;
1834
1835         if ((setuid || setgid) &&
1836             !(setuid && !mperm->mrp_allow_setuid) &&
1837             !(setgid && !mperm->mrp_allow_setgid)) {
1838                 OBD_ALLOC(xperm, sizeof(*xperm));
1839                 if (!xperm) {
1840                         OBD_FREE(lperm, sizeof(*lperm));
1841                         return -ENOMEM;
1842                 }
1843         } else
1844                 xperm = NULL;
1845
1846         return __remote_acl_update(racl, mperm, lperm, xperm);
1847 }
1848
1849 /*
1850  * return -EACCES at any error cases
1851  */
1852 int ll_remote_acl_permission(struct inode *inode, int mode)
1853 {
1854         struct ll_sb_info *sbi = ll_i2sbi(inode);
1855         struct remote_acl *racl = ll_i2info(inode)->lli_remote_acl;
1856         struct ptlrpc_request *req = NULL;
1857         struct lustre_id id;
1858         struct mds_remote_perm *mperm;
1859         int rc = -EACCES, perm;
1860
1861         if (!racl)
1862                 return -EACCES;
1863
1864         if (__remote_acl_check(racl, &perm) == 0) {
1865                 return ((perm & mode) == mode ? 0 : -EACCES);
1866         }
1867
1868         might_sleep();
1869
1870         /* doing update
1871          */
1872         down(&racl->ra_update_sem);
1873
1874         /* we might lose the race when obtain semaphore,
1875          * so check again.
1876          */
1877         if (__remote_acl_check(racl, &perm) == 0) {
1878                 if ((perm & mode) == mode)
1879                         rc = 0;
1880                 goto out;
1881         }
1882
1883         /* really fetch from mds
1884          */
1885         ll_inode2id(&id, inode);
1886         if (md_access_check(sbi->ll_md_exp, &id, &req))
1887                 goto out;
1888
1889         /* status non-zero indicate there's more apparent error
1890          * detected by mds, e.g. didn't allow this user at all.
1891          * we simply ignore and didn't cache it.
1892          */
1893         if (req->rq_repmsg->status)
1894                 goto out;
1895
1896         mperm = lustre_swab_repbuf(req, 1, sizeof(*mperm),
1897                                    lustre_swab_remote_perm);
1898         LASSERT(mperm);
1899         LASSERT_REPSWABBED(req, 1);
1900
1901         if ((mperm->mrp_perm & mode) == mode)
1902                 rc = 0;
1903
1904         remote_acl_update_locked(racl, mperm);
1905 out:
1906         if (req)
1907                 ptlrpc_req_finished(req);
1908
1909         up(&racl->ra_update_sem);
1910         return rc;
1911 }
1912
1913 int ll_remote_acl_update(struct inode *inode, struct mds_remote_perm *perm)
1914 {
1915         struct remote_acl *racl = ll_i2info(inode)->lli_remote_acl;
1916         int rc;
1917
1918         LASSERT(perm);
1919
1920         if (!racl)
1921                 return -EACCES;
1922
1923         down(&racl->ra_update_sem);
1924         rc = remote_acl_update_locked(racl, perm);
1925         up(&racl->ra_update_sem);
1926
1927         return rc;
1928 }
1929
1930 void ll_inode_invalidate_acl(struct inode *inode)
1931 {
1932         struct ll_sb_info *sbi = ll_i2sbi(inode);
1933         struct ll_inode_info *lli = ll_i2info(inode);
1934
1935         if (sbi->ll_remote) {
1936                 struct remote_acl *racl = lli->lli_remote_acl;
1937
1938                 LASSERT(!lli->lli_posix_acl);
1939                 if (racl) {
1940                         down(&racl->ra_update_sem);
1941                         spin_lock(&racl->ra_lock);
1942                         remote_acl_flush(lli->lli_remote_acl);
1943                         spin_unlock(&racl->ra_lock);
1944                         up(&racl->ra_update_sem);
1945                 }
1946         } else {
1947                 /* we can't invalide acl here: suppose we touch a new file
1948                  * under a dir, blocking ast on dir will lead to open failure
1949                  * on client, although succeed on mds. it's kind of weird,
1950                  * the real fix i think is improve client-vfs interaction.
1951                  *
1952                  * currently we just do nothing here.
1953                  */
1954                 return;
1955
1956                 LASSERT(!lli->lli_remote_acl);
1957                 spin_lock(&lli->lli_lock);
1958                 posix_acl_release(lli->lli_posix_acl);
1959                 lli->lli_posix_acl = NULL;
1960                 spin_unlock(&lli->lli_lock);
1961         }
1962 }
1963
1964 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1965 {
1966         struct ll_inode_info *lli = ll_i2info(inode);
1967         struct lov_stripe_md *lsm = md->lsm;
1968         struct mds_body *body = md->body;
1969         struct mea *mea = md->mea;
1970         struct posix_acl *posix_acl = md->posix_acl;
1971         struct ll_sb_info *sbi = ll_i2sbi(inode);
1972         struct lustre_key *mkey = md->key;
1973         ENTRY;
1974
1975         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1976
1977         if (md->lsm && md->lsm->lsm_magic != LOV_MAGIC) {
1978                 /* check for default striping info for dir. */
1979                 LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1980         }
1981         
1982         if (lsm != NULL) {
1983                 LASSERT(lsm->lsm_object_gr > 0);
1984                 if (lli->lli_smd == NULL) {
1985                         lli->lli_smd = lsm;
1986                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1987                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1988                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1989                 } else {
1990                         int i;
1991                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1992                                 CERROR("lsm mismatch for inode %ld\n",
1993                                        inode->i_ino);
1994                                 CERROR("lli_smd:\n");
1995                                 dump_lsm(D_ERROR, lli->lli_smd);
1996                                 CERROR("lsm:\n");
1997                                 dump_lsm(D_ERROR, lsm);
1998                                 LBUG();
1999                         }
2000                         /* XXX FIXME -- We should decide on a safer (atomic) and
2001                          * more elegant way to update the lsm */
2002                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
2003                                 lli->lli_smd->lsm_oinfo[i].loi_id =
2004                                         lsm->lsm_oinfo[i].loi_id;
2005                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
2006                                         lsm->lsm_oinfo[i].loi_gr;
2007                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
2008                                         lsm->lsm_oinfo[i].loi_ost_idx;
2009                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
2010                                         lsm->lsm_oinfo[i].loi_ost_gen;
2011                         }
2012                 }
2013                 /* bug 2844 - limit i_blksize for broken user-space apps */
2014                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
2015                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
2016                 if (lli->lli_smd != lsm)
2017                         obd_free_memmd(ll_i2dtexp(inode), &lsm);
2018         }
2019
2020         if (mea != NULL) {
2021                 if (lli->lli_mea == NULL) {
2022                         lli->lli_mea = mea;
2023                 } else {
2024                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
2025                                 CERROR("mea mismatch for inode %lu\n",
2026                                         inode->i_ino);
2027                                 LBUG();
2028                         }
2029                 }
2030                 if (lli->lli_mea != mea)
2031                         obd_free_memmd(ll_i2mdexp(inode),
2032                                        (struct lov_stripe_md **) &mea);
2033         }
2034
2035         if (body->valid & OBD_MD_FID)
2036                 id_assign_fid(&lli->lli_id, &body->id1);
2037         
2038         if (body->valid & OBD_MD_FLID)
2039                 id_ino(&lli->lli_id) = id_ino(&body->id1);
2040
2041         if (body->valid & OBD_MD_FLGENER)
2042                 id_gen(&lli->lli_id) = id_gen(&body->id1);
2043
2044         /* local/remote ACL */
2045         if (sbi->ll_remote) {
2046                 LASSERT(md->posix_acl == NULL);
2047                 if (md->remote_perm) {
2048                         ll_remote_acl_update(inode, md->remote_perm);
2049                         OBD_FREE(md->remote_perm, sizeof(*md->remote_perm));
2050                         md->remote_perm = NULL;
2051                 }
2052         } else {
2053                 LASSERT(md->remote_perm == NULL);
2054                 spin_lock(&lli->lli_lock);
2055                 if (posix_acl != NULL) {
2056                         if (lli->lli_posix_acl != NULL)
2057                                 posix_acl_release(lli->lli_posix_acl);
2058                         lli->lli_posix_acl = posix_acl;
2059                 }
2060                 spin_unlock(&lli->lli_lock);
2061         }
2062
2063         if (body->valid & OBD_MD_FLID)
2064                 inode->i_ino = id_ino(&body->id1);
2065         if (body->valid & OBD_MD_FLGENER)
2066                 inode->i_generation = id_gen(&body->id1);
2067         if (body->valid & OBD_MD_FLATIME)
2068                 LTIME_S(inode->i_atime) = body->atime;
2069         if (body->valid & OBD_MD_FLMTIME &&
2070             body->mtime > LTIME_S(inode->i_mtime)) {
2071                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
2072                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
2073                 LTIME_S(inode->i_mtime) = body->mtime;
2074         }
2075         if (body->valid & OBD_MD_FLCTIME &&
2076             body->ctime > LTIME_S(inode->i_ctime))
2077                 LTIME_S(inode->i_ctime) = body->ctime;
2078         if (body->valid & OBD_MD_FLMODE) {
2079                 inode->i_mode = (inode->i_mode & S_IFMT) |
2080                         (body->mode & ~S_IFMT);
2081         }
2082         if (body->valid & OBD_MD_FLTYPE) {
2083                 inode->i_mode = (inode->i_mode & ~S_IFMT) |
2084                         (body->mode & S_IFMT);
2085         }
2086         if (body->valid & OBD_MD_FLUID)
2087                 inode->i_uid = body->uid;
2088         if (body->valid & OBD_MD_FLGID)
2089                 inode->i_gid = body->gid;
2090         if (body->valid & OBD_MD_FLFLAGS)
2091                 inode->i_flags = body->flags;
2092         if (body->valid & OBD_MD_FLNLINK)
2093                 inode->i_nlink = body->nlink;
2094         if (body->valid & OBD_MD_FLRDEV)
2095 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2096                 inode->i_rdev = body->rdev;
2097 #else
2098                 inode->i_rdev = old_decode_dev(body->rdev);
2099 #endif
2100         if (body->valid & OBD_MD_FLSIZE)
2101                 inode->i_size = body->size;
2102         if (body->valid & OBD_MD_FLBLOCKS)
2103                 inode->i_blocks = body->blocks;
2104
2105         if (body->valid & OBD_MD_FLSIZE)
2106                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
2107
2108         if (body->valid & OBD_MD_FLAUDIT) {
2109                 struct ll_sb_info * sbi = ll_s2sbi(inode->i_sb);
2110                 if (IS_AUDIT_OP(body->audit, AUDIT_FS))
2111                         sbi->ll_audit_mask = body->audit;
2112                 else
2113                         lli->lli_audit_mask = body->audit;
2114         }
2115
2116         if (mkey != NULL) {
2117                 LASSERT(body->valid & OBD_MD_FLKEY);
2118                 ll_crypto_init_inode_key(inode, mkey);  
2119         }
2120
2121 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2122         inode->i_dev = (kdev_t)id_group(&lli->lli_id);
2123 #endif
2124         LASSERT(id_fid(&lli->lli_id) != 0);
2125 }
2126
2127 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
2128 static struct backing_dev_info ll_backing_dev_info = {
2129         .ra_pages       = 0,    /* No readahead */
2130         .memory_backed  = 0,    /* Does contribute to dirty memory */
2131 };
2132 #endif
2133
2134 void ll_read_inode2(struct inode *inode, void *opaque)
2135 {
2136         struct lustre_md *md = opaque;
2137         struct ll_inode_info *lli = ll_i2info(inode);
2138         ENTRY;
2139
2140         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
2141                inode->i_generation, inode);
2142
2143         ll_lli_init(lli);
2144
2145         LASSERT(!lli->lli_smd);
2146
2147         if (ll_i2sbi(inode)->ll_remote) {
2148                 lli->lli_remote_acl = remote_acl_alloc();
2149                 /* if failed alloc, nobody will be able to access this inode */
2150         }
2151
2152         /* Core attributes from the MDS first.  This is a new inode, and
2153          * the VFS doesn't zero times in the core inode so we have to do
2154          * it ourselves.  They will be overwritten by either MDS or OST
2155          * attributes - we just need to make sure they aren't newer. */
2156         LTIME_S(inode->i_mtime) = 0;
2157         LTIME_S(inode->i_atime) = 0;
2158         LTIME_S(inode->i_ctime) = 0;
2159
2160         inode->i_rdev = 0;
2161         ll_update_inode(inode, md);
2162
2163         /* OIDEBUG(inode); */
2164
2165         if (S_ISREG(inode->i_mode)) {
2166                 inode->i_op = &ll_file_inode_operations;
2167                 inode->i_fop = &ll_file_operations;
2168                 inode->i_mapping->a_ops = &ll_aops;
2169                 EXIT;
2170         } else if (S_ISDIR(inode->i_mode)) {
2171                 inode->i_op = &ll_dir_inode_operations;
2172                 inode->i_fop = &ll_dir_operations;
2173                 inode->i_mapping->a_ops = &ll_dir_aops;
2174                 EXIT;
2175         } else if (S_ISLNK(inode->i_mode)) {
2176                 inode->i_op = &ll_fast_symlink_inode_operations;
2177                 EXIT;
2178         } else {
2179                 inode->i_op = &ll_special_inode_operations;
2180
2181 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2182                 init_special_inode(inode, inode->i_mode,
2183                                    kdev_t_to_nr(inode->i_rdev));
2184
2185                 /* initializing backing dev info. */
2186                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
2187 #else
2188                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
2189 #endif
2190                 lli->ll_save_ifop = inode->i_fop;
2191
2192                 if (S_ISCHR(inode->i_mode))
2193                         inode->i_fop = &ll_special_chr_inode_fops;
2194                 else if (S_ISBLK(inode->i_mode))
2195                         inode->i_fop = &ll_special_blk_inode_fops;
2196                 else if (S_ISFIFO(inode->i_mode))
2197                         inode->i_fop = &ll_special_fifo_inode_fops;
2198                 else if (S_ISSOCK(inode->i_mode))
2199                         inode->i_fop = &ll_special_sock_inode_fops;
2200
2201                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
2202                       inode->i_fop);
2203
2204                 if (lli->ll_save_ifop->owner) {
2205                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
2206                               lli->ll_save_ifop->owner);
2207                 }
2208                 EXIT;
2209         }
2210 }
2211
2212 void ll_delete_inode(struct inode *inode)
2213 {
2214         struct ll_sb_info *sbi = ll_i2sbi(inode);
2215         struct lustre_id id;
2216         int rc;
2217         ENTRY;
2218
2219         ll_inode2id(&id, inode);
2220
2221         rc = md_delete_inode(sbi->ll_md_exp, &id);
2222         if (rc) {
2223                 CERROR("md_delete_inode() failed, error %d\n", 
2224                        rc);
2225         }
2226
2227         clear_inode(inode);
2228         EXIT;
2229 }
2230
2231 int ll_iocontrol(struct inode *inode, struct file *file,
2232                  unsigned int cmd, unsigned long arg)
2233 {
2234         struct ll_sb_info *sbi = ll_i2sbi(inode);
2235         struct ptlrpc_request *req = NULL;
2236         int rc, flags = 0;
2237         ENTRY;
2238
2239         switch(cmd) {
2240         case EXT3_IOC_GETFLAGS: {
2241                 struct lustre_id id;
2242                 __u64 valid = OBD_MD_FLFLAGS;
2243                 struct mds_body *body;
2244
2245                 ll_inode2id(&id, inode);
2246                 rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, NULL,
2247                                 0, 0, NULL, &req);
2248                 if (rc) {
2249                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
2250                         RETURN(-abs(rc));
2251                 }
2252
2253                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
2254
2255                 if (body->flags & S_APPEND)
2256                         flags |= EXT3_APPEND_FL;
2257                 if (body->flags & S_IMMUTABLE)
2258                         flags |= EXT3_IMMUTABLE_FL;
2259                 if (body->flags & S_NOATIME)
2260                         flags |= EXT3_NOATIME_FL;
2261
2262                 ptlrpc_req_finished (req);
2263
2264                 RETURN(put_user(flags, (int *)arg));
2265         }
2266         case EXT3_IOC_SETFLAGS: {
2267                 struct mdc_op_data *op_data;
2268                 struct iattr attr;
2269                 struct obdo *oa;
2270                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
2271
2272                 if (get_user(flags, (int *)arg))
2273                         RETURN(-EFAULT);
2274
2275                 oa = obdo_alloc();
2276                 if (!oa)
2277                         RETURN(-ENOMEM);
2278
2279                 OBD_ALLOC(op_data, sizeof(*op_data));
2280                 if (op_data == NULL) {
2281                         obdo_free(oa);
2282                         RETURN(-ENOMEM);
2283                 }
2284                 ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
2285
2286                 memset(&attr, 0x0, sizeof(attr));
2287                 attr.ia_attr_flags = flags;
2288                 attr.ia_valid |= ATTR_ATTR_FLAG;
2289
2290                 rc = md_setattr(sbi->ll_md_exp, op_data,
2291                                 &attr, NULL, 0, NULL, 0, NULL, 0, &req);
2292                 OBD_FREE(op_data, sizeof(*op_data));
2293                 if (rc) {
2294                         ptlrpc_req_finished(req);
2295                         if (rc != -EPERM && rc != -EACCES)
2296                                 CERROR("md_setattr fails: rc = %d\n", rc);
2297                         obdo_free(oa);
2298                         RETURN(rc);
2299                 }
2300                 ptlrpc_req_finished(req);
2301
2302                 oa->o_id = lsm->lsm_object_id;
2303                 oa->o_gr = lsm->lsm_object_gr;
2304                 oa->o_flags = flags;
2305                 *(obdo_id(oa)) = ll_i2info(inode)->lli_id;
2306                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP 
2307                               | OBD_MD_FLIFID;
2308
2309                 rc = obd_setattr(sbi->ll_dt_exp, oa, lsm, NULL, NULL);
2310                 obdo_free(oa);
2311                 if (rc) {
2312                         if (rc != -EPERM && rc != -EACCES)
2313                                 CERROR("md_setattr fails: rc = %d\n", rc);
2314                         RETURN(rc);
2315                 }
2316
2317                 if (flags & EXT3_APPEND_FL)
2318                         inode->i_flags |= S_APPEND;
2319                 else
2320                         inode->i_flags &= ~S_APPEND;
2321                 if (flags & EXT3_IMMUTABLE_FL)
2322                         inode->i_flags |= S_IMMUTABLE;
2323                 else
2324                         inode->i_flags &= ~S_IMMUTABLE;
2325                 if (flags & EXT3_NOATIME_FL)
2326                         inode->i_flags |= S_NOATIME;
2327                 else
2328                         inode->i_flags &= ~S_NOATIME;
2329
2330                 RETURN(0);
2331         }
2332         default:
2333                 RETURN(-ENOSYS);
2334         }
2335
2336         RETURN(0);
2337 }
2338
2339 /* this is only called in the case of forced umount. */
2340 void ll_umount_begin(struct super_block *sb)
2341 {
2342         struct ll_sb_info *sbi = ll_s2sbi(sb);
2343         struct obd_ioctl_data ioc_data = { 0 };
2344         struct obd_device *obd;
2345         ENTRY;
2346      
2347         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2348                sb->s_count, atomic_read(&sb->s_active));
2349         
2350         obd = class_exp2obd(sbi->ll_md_exp);
2351         if (obd == NULL) {
2352                 CERROR("Invalid MDC connection handle "LPX64"\n",
2353                        sbi->ll_md_exp->exp_handle.h_cookie);
2354                 EXIT;
2355                 return;
2356         }
2357         obd->obd_no_recov = 1;
2358         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp,
2359                       sizeof(ioc_data), &ioc_data, NULL);
2360
2361         obd = class_exp2obd(sbi->ll_dt_exp);
2362         if (obd == NULL) {
2363                 CERROR("Invalid LOV connection handle "LPX64"\n",
2364                        sbi->ll_dt_exp->exp_handle.h_cookie);
2365                 EXIT;
2366                 return;
2367         }
2368
2369         obd->obd_no_recov = 1;
2370         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp,
2371                       sizeof(ioc_data), &ioc_data, NULL);
2372
2373         /*
2374          * really, we'd like to wait until there are no requests outstanding,
2375          * and then continue.  For now, we just invalidate the requests,
2376          * schedule, and hope.
2377          */
2378         schedule();
2379
2380         EXIT;
2381 }
2382
2383 int ll_prep_inode(struct obd_export *dt_exp, struct obd_export *md_exp,
2384                   struct inode **inode, struct ptlrpc_request *req,
2385                   int offset, struct super_block *sb)
2386 {
2387         struct lustre_md md;
2388         int rc = 0;
2389
2390         rc = mdc_req2lustre_md(md_exp, req, offset, dt_exp, &md);
2391         if (rc)
2392                 RETURN(rc);
2393
2394         if (*inode) {
2395                 ll_update_inode(*inode, &md);
2396         } else {
2397                 LASSERT(sb);
2398                 *inode = ll_iget(sb, id_ino(&md.body->id1), &md);
2399                 if (*inode == NULL || is_bad_inode(*inode)) {
2400                         /* free the lsm if we allocated one above */
2401                         if (md.lsm != NULL)
2402                                 obd_free_memmd(dt_exp, &md.lsm);
2403                         if (md.mea != NULL)
2404                                 obd_free_memmd(md_exp,
2405                                                (struct lov_stripe_md**)&md.mea);
2406                         rc = -ENOMEM;
2407                         CERROR("new_inode -fatal: rc %d\n", rc);
2408                 }
2409         }
2410
2411         RETURN(rc);
2412 }
2413
2414 int ll_show_options(struct seq_file *m, struct vfsmount *mnt)
2415 {
2416         struct ll_sb_info *sbi = ll_s2sbi(mnt->mnt_sb);
2417         struct lustre_mount_data *lmd = sbi->ll_lmd;
2418
2419         if (lmd) {
2420                 seq_printf(m, ",mds_sec=%s,oss_sec=%s",
2421                            lmd->lmd_mds_security, lmd->lmd_oss_security);
2422         }
2423         seq_printf(m, ",%s", sbi->ll_remote ? "remote" : "local");
2424         if (sbi->ll_remote && lmd)
2425                 seq_printf(m, ",nllu=%u:%u", lmd->lmd_nllu, lmd->lmd_nllg);
2426
2427         if (lmd && lmd->lmd_pag)
2428                 seq_printf(m, ",pag");
2429
2430         return 0;
2431 }
2432
2433 int ll_get_fid(struct obd_export *exp, struct lustre_id *idp,
2434                char *filename, struct lustre_id *ret)
2435 {
2436         struct ptlrpc_request *request = NULL;
2437         struct mds_body *body;
2438         int rc;
2439
2440         rc = md_getattr_lock(exp, idp, filename, strlen(filename) + 1,
2441                              OBD_MD_FID, 0, &request);
2442         if (rc < 0) {
2443                 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
2444                        filename, rc);
2445                 return rc;
2446         }
2447
2448         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
2449         LASSERT(body != NULL);
2450         LASSERT_REPSWABBED(request, 0);
2451
2452         *ret = body->id1;
2453         ptlrpc_req_finished(request);
2454
2455         return rc;
2456 }
2457 int ll_flush_cred(struct inode *inode)
2458 {
2459         struct ll_sb_info *sbi = ll_i2sbi(inode);
2460         int rc = 0;
2461
2462         /* XXX to avoid adding api, we simply use set_info() interface
2463          * to notify underlying obds. set_info() is more like a ioctl() now...
2464          */
2465         if (sbi->ll_md_exp) {
2466                 rc = obd_set_info(sbi->ll_md_exp,
2467                                   strlen("flush_cred"), "flush_cred",
2468                                   0, NULL);
2469                 if (rc)
2470                         return rc;
2471         }
2472
2473         if (sbi->ll_dt_exp) {
2474                 rc = obd_set_info(sbi->ll_dt_exp,
2475                                   strlen("flush_cred"), "flush_cred",
2476                                   0, NULL);
2477                 if (rc)
2478                         return rc;
2479         }
2480
2481         return rc;
2482 }