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