Whamcloud - gitweb
land b1_4_smallfix on b1_4(20050202_1817)
[fs/lustre-release.git] / lustre / llite / llite_nfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   NFS export of Lustre Light File System 
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 #include <linux/lustre_lite.h>
26 #include "llite_internal.h"
27
28 __u32 get_uuid2int(const char *name, int len)
29 {
30         __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
31         while (len--) {
32                 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
33                 if (key & 0x80000000) key -= 0x7fffffff;
34                 key1 = key0;
35                 key0 = key;
36         }
37         return (key0 << 1);
38 }
39
40 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
41 static int ll_nfs_test_inode(struct inode *inode, unsigned long ino, void *opaque)
42 #else
43 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
44 #endif
45 {
46         struct ll_fid *iid = opaque;
47
48         if (inode->i_ino == iid->id && inode->i_generation == iid->generation)
49                 return 1;
50
51         return 0;
52 }
53
54 static struct inode * search_inode_for_lustre(struct super_block *sb,
55                                               unsigned long ino,
56                                               unsigned long generation,
57                                               int mode)
58 {
59         struct ptlrpc_request *req = NULL;
60         struct ll_sb_info *sbi = ll_s2sbi(sb);
61         struct ll_fid fid;
62         unsigned long valid = 0;
63         int eadatalen = 0, rc;
64         struct inode *inode = NULL;
65         struct ll_fid iid = { .id = ino, .generation = generation };
66
67         inode = ILOOKUP(sb, ino, ll_nfs_test_inode, &iid);
68
69         if (inode)
70                 return inode;
71         if (S_ISREG(mode)) {
72                 eadatalen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
73                 valid |= OBD_MD_FLEASIZE;
74         }
75         fid.id = (__u64)ino;
76         fid.generation = generation;
77         fid.f_type = mode;
78
79         rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, eadatalen, &req);
80         if (rc) {
81                 CERROR("failure %d inode %lu\n", rc, ino);
82                 return ERR_PTR(rc);
83         }
84
85         rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, sb);
86         if (rc) {
87                 ptlrpc_req_finished(req);
88                 return ERR_PTR(rc);
89         }
90         ptlrpc_req_finished(req);
91
92         return inode;
93 }
94
95 extern struct dentry_operations ll_d_ops;
96
97 static struct dentry *ll_iget_for_nfs(struct super_block *sb, unsigned long ino,
98                                       __u32 generation, umode_t mode)
99 {
100         struct inode *inode;
101         struct dentry *result;
102         struct list_head *lp;
103
104         if (ino == 0)
105                 return ERR_PTR(-ESTALE);
106
107         inode = search_inode_for_lustre(sb, ino, generation, mode);
108         if (IS_ERR(inode)) {
109                 return ERR_PTR(PTR_ERR(inode));
110         }
111         if (is_bad_inode(inode) ||
112             (generation && inode->i_generation != generation)){
113                 /* we didn't find the right inode.. */
114                 CERROR("Inode %lu, Bad count: %lu %d or version  %u %u\n",
115                        inode->i_ino, (unsigned long)inode->i_nlink,
116                        atomic_read(&inode->i_count), inode->i_generation,
117                        generation);
118                 iput(inode);
119                 return ERR_PTR(-ESTALE);
120         }
121
122         /* now to find a dentry.
123          * If possible, get a well-connected one
124          */
125         spin_lock(&dcache_lock);
126         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
127                 result = list_entry(lp,struct dentry, d_alias);
128                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
129                         dget_locked(result);
130                         ll_set_dflags(result, DCACHE_REFERENCED);
131                         spin_unlock(&dcache_lock);
132                         iput(inode);
133                         return result;
134                 }
135         }
136         spin_unlock(&dcache_lock);
137         result = d_alloc_root(inode);
138         if (result == NULL) {
139                 iput(inode);
140                 return ERR_PTR(-ENOMEM);
141         }
142         result->d_flags |= DCACHE_DISCONNECTED;
143
144         ll_set_dd(result);
145         result->d_op = &ll_d_ops;
146         return result;
147 }
148
149 struct dentry *ll_fh_to_dentry(struct super_block *sb, __u32 *data, int len,
150                                int fhtype, int parent)
151 {
152         switch (fhtype) {
153                 case 2:
154                         if (len < 5)
155                                 break;
156                         if (parent)
157                                 return ll_iget_for_nfs(sb, data[3], 0, data[4]);
158                 case 1:
159                         if (len < 3)
160                                 break;
161                         if (parent)
162                                 break;
163                         return ll_iget_for_nfs(sb, data[0], data[1], data[2]);
164                 default: break;
165         }
166         return ERR_PTR(-EINVAL);
167 }
168
169 int ll_dentry_to_fh(struct dentry *dentry, __u32 *datap, int *lenp,
170                     int need_parent)
171 {
172         if (*lenp < 3)
173                 return 255;
174         *datap++ = dentry->d_inode->i_ino;
175         *datap++ = dentry->d_inode->i_generation;
176         *datap++ = (__u32)(S_IFMT & dentry->d_inode->i_mode);
177
178         if (*lenp == 3 || S_ISDIR(dentry->d_inode->i_mode)) {
179                 *lenp = 3;
180                 return 1;
181         }
182         if (dentry->d_parent) {
183                 *datap++ = dentry->d_parent->d_inode->i_ino;
184                 *datap++ = (__u32)(S_IFMT & dentry->d_parent->d_inode->i_mode);
185
186                 *lenp = 5;
187                 return 2;
188         }
189         *lenp = 3;
190         return 1;
191 }