Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[fs/lustre-release.git] / lustre / obdclass / linux / linux-obdo.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/linux/linux-obdo.c
37  *
38  * Object Devices Class Driver
39  * These are the only exported functions, they provide some generic
40  * infrastructure for managing object devices
41  */
42
43 #define DEBUG_SUBSYSTEM S_CLASS
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 #ifndef __KERNEL__
49 #include <liblustre.h>
50 #else
51 #include <linux/module.h>
52 #include <obd_class.h>
53 #include <lustre/lustre_idl.h>
54 #endif
55
56 #ifdef __KERNEL__
57 #include <linux/fs.h>
58 #include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
59
60 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
61 {
62         if (ia_valid & ATTR_ATIME) {
63                 oa->o_atime = LTIME_S(attr->ia_atime);
64                 oa->o_valid |= OBD_MD_FLATIME;
65         }
66         if (ia_valid & ATTR_MTIME) {
67                 oa->o_mtime = LTIME_S(attr->ia_mtime);
68                 oa->o_valid |= OBD_MD_FLMTIME;
69         }
70         if (ia_valid & ATTR_CTIME) {
71                 oa->o_ctime = LTIME_S(attr->ia_ctime);
72                 oa->o_valid |= OBD_MD_FLCTIME;
73         }
74         if (ia_valid & ATTR_SIZE) {
75                 oa->o_size = attr->ia_size;
76                 oa->o_valid |= OBD_MD_FLSIZE;
77         }
78         if (ia_valid & ATTR_MODE) {
79                 oa->o_mode = attr->ia_mode;
80                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
81                 if (!in_group_p(oa->o_gid) && !cfs_capable(CFS_CAP_FSETID))
82                         oa->o_mode &= ~S_ISGID;
83         }
84         if (ia_valid & ATTR_UID) {
85                 oa->o_uid = attr->ia_uid;
86                 oa->o_valid |= OBD_MD_FLUID;
87         }
88         if (ia_valid & ATTR_GID) {
89                 oa->o_gid = attr->ia_gid;
90                 oa->o_valid |= OBD_MD_FLGID;
91         }
92 }
93 EXPORT_SYMBOL(obdo_from_iattr);
94
95 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
96 {
97         valid &= oa->o_valid;
98
99         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
100                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
101                        oa->o_valid, oa->o_mtime, oa->o_ctime);
102
103         attr->ia_valid = 0;
104         if (valid & OBD_MD_FLATIME) {
105                 LTIME_S(attr->ia_atime) = oa->o_atime;
106                 attr->ia_valid |= ATTR_ATIME;
107         }
108         if (valid & OBD_MD_FLMTIME) {
109                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
110                 attr->ia_valid |= ATTR_MTIME;
111         }
112         if (valid & OBD_MD_FLCTIME) {
113                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
114                 attr->ia_valid |= ATTR_CTIME;
115         }
116         if (valid & OBD_MD_FLSIZE) {
117                 attr->ia_size = oa->o_size;
118                 attr->ia_valid |= ATTR_SIZE;
119         }
120 #if 0   /* you shouldn't be able to change a file's type with setattr */
121         if (valid & OBD_MD_FLTYPE) {
122                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
123                 attr->ia_valid |= ATTR_MODE;
124         }
125 #endif
126         if (valid & OBD_MD_FLMODE) {
127                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
128                 attr->ia_valid |= ATTR_MODE;
129                 if (!in_group_p(oa->o_gid) && !cfs_capable(CFS_CAP_FSETID))
130                         attr->ia_mode &= ~S_ISGID;
131         }
132         if (valid & OBD_MD_FLUID) {
133                 attr->ia_uid = oa->o_uid;
134                 attr->ia_valid |= ATTR_UID;
135         }
136         if (valid & OBD_MD_FLGID) {
137                 attr->ia_gid = oa->o_gid;
138                 attr->ia_valid |= ATTR_GID;
139         }
140 }
141 EXPORT_SYMBOL(iattr_from_obdo);
142
143 /* WARNING: the file systems must take care not to tinker with
144    attributes they don't manage (such as blocks). */
145 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
146 {
147         obd_flag newvalid = 0;
148
149         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
150                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
151                        valid, LTIME_S(src->i_mtime), 
152                        LTIME_S(src->i_ctime));
153
154         if (valid & OBD_MD_FLATIME) {
155                 dst->o_atime = LTIME_S(src->i_atime);
156                 newvalid |= OBD_MD_FLATIME;
157         }
158         if (valid & OBD_MD_FLMTIME) {
159                 dst->o_mtime = LTIME_S(src->i_mtime);
160                 newvalid |= OBD_MD_FLMTIME;
161         }
162         if (valid & OBD_MD_FLCTIME) {
163                 dst->o_ctime = LTIME_S(src->i_ctime);
164                 newvalid |= OBD_MD_FLCTIME;
165         }
166         if (valid & OBD_MD_FLSIZE) {
167                 dst->o_size = i_size_read(src);
168                 newvalid |= OBD_MD_FLSIZE;
169         }
170         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
171                 dst->o_blocks = src->i_blocks;
172                 newvalid |= OBD_MD_FLBLOCKS;
173         }
174         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
175                 dst->o_blksize = 1<<src->i_blkbits;
176                 newvalid |= OBD_MD_FLBLKSZ;
177         }
178         if (valid & OBD_MD_FLTYPE) {
179                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
180                 newvalid |= OBD_MD_FLTYPE;
181         }
182         if (valid & OBD_MD_FLMODE) {
183                 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
184                 newvalid |= OBD_MD_FLMODE;
185         }
186         if (valid & OBD_MD_FLUID) {
187                 dst->o_uid = src->i_uid;
188                 newvalid |= OBD_MD_FLUID;
189         }
190         if (valid & OBD_MD_FLGID) {
191                 dst->o_gid = src->i_gid;
192                 newvalid |= OBD_MD_FLGID;
193         }
194         if (valid & OBD_MD_FLFLAGS) {
195                 dst->o_flags = src->i_flags;
196                 newvalid |= OBD_MD_FLFLAGS;
197         }
198         if (valid & OBD_MD_FLGENER) {
199                 dst->o_generation = src->i_generation;
200                 newvalid |= OBD_MD_FLGENER;
201         }
202         if (valid & OBD_MD_FLFID) {
203                 dst->o_fid = src->i_ino;
204                 newvalid |= OBD_MD_FLFID;
205         }
206
207         dst->o_valid |= newvalid;
208 }
209 EXPORT_SYMBOL(obdo_from_inode);
210
211 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
212 {
213         valid &= src->o_valid;
214
215         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
216                 CDEBUG(D_INODE,
217                        "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
218                        src->o_valid, LTIME_S(dst->i_mtime),
219                        LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
220
221         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
222                 LTIME_S(dst->i_atime) = src->o_atime;
223         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
224                 LTIME_S(dst->i_mtime) = src->o_mtime;
225         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
226                 LTIME_S(dst->i_ctime) = src->o_ctime;
227         if (valid & OBD_MD_FLSIZE)
228                 i_size_write(dst, src->o_size);
229         /* optimum IO size */
230         if (valid & OBD_MD_FLBLKSZ && src->o_blksize > (1<<dst->i_blkbits)) {
231                 dst->i_blkbits = ffs(src->o_blksize)-1;
232 #ifdef HAVE_INODE_BLKSIZE
233                 dst->i_blksize = src->o_blksize;
234 #endif
235         }
236
237         if (dst->i_blkbits < CFS_PAGE_SHIFT) {
238 #ifdef HAVE_INODE_BLKSIZE
239                 dst->i_blksize = CFS_PAGE_SIZE;
240 #endif
241                 dst->i_blkbits = CFS_PAGE_SHIFT;
242         }
243
244         /* allocation of space */
245         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
246                 dst->i_blocks = src->o_blocks;
247 }
248 EXPORT_SYMBOL(obdo_refresh_inode);
249
250 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
251 {
252         valid &= src->o_valid;
253
254         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
255                 CDEBUG(D_INODE,
256                        "valid "LPX64", cur time %lu/%lu, new "LPU64"/"LPU64"\n",
257                        src->o_valid, LTIME_S(dst->i_mtime),
258                        LTIME_S(dst->i_ctime), src->o_mtime, src->o_ctime);
259
260         if (valid & OBD_MD_FLATIME)
261                 LTIME_S(dst->i_atime) = src->o_atime;
262         if (valid & OBD_MD_FLMTIME)
263                 LTIME_S(dst->i_mtime) = src->o_mtime;
264         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
265                 LTIME_S(dst->i_ctime) = src->o_ctime;
266         if (valid & OBD_MD_FLSIZE)
267                 i_size_write(dst, src->o_size);
268         if (valid & OBD_MD_FLBLOCKS) { /* allocation of space */
269                 dst->i_blocks = src->o_blocks;
270                 if (dst->i_blocks < src->o_blocks) /* overflow */
271                         dst->i_blocks = -1;
272
273         }
274         if (valid & OBD_MD_FLBLKSZ) {
275                 dst->i_blkbits = ffs(src->o_blksize)-1;
276 #ifdef HAVE_INODE_BLKSIZE
277                 dst->i_blksize = src->o_blksize;
278 #endif
279         }
280         if (valid & OBD_MD_FLTYPE)
281                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
282         if (valid & OBD_MD_FLMODE)
283                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
284         if (valid & OBD_MD_FLUID)
285                 dst->i_uid = src->o_uid;
286         if (valid & OBD_MD_FLGID)
287                 dst->i_gid = src->o_gid;
288         if (valid & OBD_MD_FLFLAGS)
289                 dst->i_flags = src->o_flags;
290         if (valid & OBD_MD_FLGENER)
291                 dst->i_generation = src->o_generation;
292 }
293 EXPORT_SYMBOL(obdo_to_inode);
294 #endif