]> granicus.if.org Git - zfs/blob - include/linux/vfs_compat.h
cstyle: Resolve C style issues
[zfs] / include / linux / vfs_compat.h
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24  */
25
26 #ifndef _ZFS_VFS_H
27 #define _ZFS_VFS_H
28
29 /*
30  * 2.6.28 API change,
31  * Added insert_inode_locked() helper function, prior to this most callers
32  * used insert_inode_hash().  The older method doesn't check for collisions
33  * in the inode_hashtable but it still acceptible for use.
34  */
35 #ifndef HAVE_INSERT_INODE_LOCKED
36 static inline int
37 insert_inode_locked(struct inode *ip)
38 {
39         insert_inode_hash(ip);
40         return (0);
41 }
42 #endif /* HAVE_INSERT_INODE_LOCKED */
43
44 /*
45  * 2.6.35 API change,
46  * Add truncate_setsize() if it is not exported by the Linux kernel.
47  *
48  * Truncate the inode and pages associated with the inode. The pages are
49  * unmapped and removed from cache.
50  */
51 #ifndef HAVE_TRUNCATE_SETSIZE
52 static inline void
53 truncate_setsize(struct inode *ip, loff_t new)
54 {
55         struct address_space *mapping = ip->i_mapping;
56
57         i_size_write(ip, new);
58
59         unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
60         truncate_inode_pages(mapping, new);
61         unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
62 }
63 #endif /* HAVE_TRUNCATE_SETSIZE */
64
65 #if defined(HAVE_BDI) && !defined(HAVE_BDI_SETUP_AND_REGISTER)
66 /*
67  * 2.6.34 API change,
68  * Add bdi_setup_and_register() function if not yet provided by kernel.
69  * It is used to quickly initialize and register a BDI for the filesystem.
70  */
71 extern atomic_long_t zfs_bdi_seq;
72
73 static inline int
74 bdi_setup_and_register(
75         struct backing_dev_info *bdi,
76         char *name,
77         unsigned int cap)
78 {
79         char tmp[32];
80         int error;
81
82         bdi->name = name;
83         bdi->capabilities = cap;
84         error = bdi_init(bdi);
85         if (error)
86                 return (error);
87
88         sprintf(tmp, "%.28s%s", name, "-%d");
89         error = bdi_register(bdi, NULL, tmp,
90             atomic_long_inc_return(&zfs_bdi_seq));
91         if (error) {
92                 bdi_destroy(bdi);
93                 return (error);
94         }
95
96         return (error);
97 }
98 #endif /* HAVE_BDI && !HAVE_BDI_SETUP_AND_REGISTER */
99
100 /*
101  * 2.6.38 API change,
102  * LOOKUP_RCU flag introduced to distinguish rcu-walk from ref-walk cases.
103  */
104 #ifndef LOOKUP_RCU
105 #define LOOKUP_RCU      0x0
106 #endif /* LOOKUP_RCU */
107
108 /*
109  * 3.2-rc1 API change,
110  * Add set_nlink() if it is not exported by the Linux kernel.
111  *
112  * i_nlink is read-only in Linux 3.2, but it can be set directly in
113  * earlier kernels.
114  */
115 #ifndef HAVE_SET_NLINK
116 static inline void
117 set_nlink(struct inode *inode, unsigned int nlink)
118 {
119         inode->i_nlink = nlink;
120 }
121 #endif /* HAVE_SET_NLINK */
122
123 /*
124  * 3.3 API change,
125  * The VFS .create, .mkdir and .mknod callbacks were updated to take a
126  * umode_t type rather than an int.  To cleanly handle both definitions
127  * the zpl_umode_t type is introduced and set accordingly.
128  */
129 #ifdef HAVE_MKDIR_UMODE_T
130 typedef umode_t         zpl_umode_t;
131 #else
132 typedef int             zpl_umode_t;
133 #endif
134
135 /*
136  * 3.5 API change,
137  * The clear_inode() function replaces end_writeback() and introduces an
138  * ordering change regarding when the inode_sync_wait() occurs.  See the
139  * configure check in config/kernel-clear-inode.m4 for full details.
140  */
141 #if defined(HAVE_EVICT_INODE) && !defined(HAVE_CLEAR_INODE)
142 #define clear_inode(ip)         end_writeback(ip)
143 #endif /* HAVE_EVICT_INODE && !HAVE_CLEAR_INODE */
144
145 /*
146  * 3.6 API change,
147  * The sget() helper function now takes the mount flags as an argument.
148  */
149 #ifdef HAVE_5ARG_SGET
150 #define zpl_sget(type, cmp, set, fl, mtd)       sget(type, cmp, set, fl, mtd)
151 #else
152 #define zpl_sget(type, cmp, set, fl, mtd)       sget(type, cmp, set, mtd)
153 #endif /* HAVE_5ARG_SGET */
154
155 #define ZFS_IOC_GETFLAGS        FS_IOC_GETFLAGS
156 #define ZFS_IOC_SETFLAGS        FS_IOC_SETFLAGS
157
158 #if defined(SEEK_HOLE) && defined(SEEK_DATA) && !defined(HAVE_LSEEK_EXECUTE)
159 static inline loff_t
160 lseek_execute(
161         struct file *filp,
162         struct inode *inode,
163         loff_t offset,
164         loff_t maxsize)
165 {
166         if (offset < 0 && !(filp->f_mode & FMODE_UNSIGNED_OFFSET))
167                 return (-EINVAL);
168
169         if (offset > maxsize)
170                 return (-EINVAL);
171
172         if (offset != filp->f_pos) {
173                 spin_lock(&filp->f_lock);
174                 filp->f_pos = offset;
175                 filp->f_version = 0;
176                 spin_unlock(&filp->f_lock);
177         }
178
179         return (offset);
180 }
181 #endif /* SEEK_HOLE && SEEK_DATA && !HAVE_LSEEK_EXECUTE */
182
183 #if defined(CONFIG_FS_POSIX_ACL)
184 /*
185  * These functions safely approximates the behavior of posix_acl_release()
186  * which cannot be used because it calls the GPL-only symbol kfree_rcu().
187  * The in-kernel version, which can access the RCU, frees the ACLs after
188  * the grace period expires.  Because we're unsure how long that grace
189  * period may be this implementation conservatively delays for 60 seconds.
190  * This is several orders of magnitude larger than expected grace period.
191  * At 60 seconds the kernel will also begin issuing RCU stall warnings.
192  */
193 #include <linux/posix_acl.h>
194 #ifndef HAVE_POSIX_ACL_CACHING
195 #define ACL_NOT_CACHED ((void *)(-1))
196 #endif /* HAVE_POSIX_ACL_CACHING */
197
198 #if defined(HAVE_POSIX_ACL_RELEASE) && !defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
199
200 #define zpl_posix_acl_release(arg)              posix_acl_release(arg)
201 #define zpl_set_cached_acl(ip, ty, n)           set_cached_acl(ip, ty, n)
202 #define zpl_forget_cached_acl(ip, ty)           forget_cached_acl(ip, ty)
203
204 #else
205
206 static inline void
207 zpl_posix_acl_free(void *arg) {
208         kfree(arg);
209 }
210
211 static inline void
212 zpl_posix_acl_release(struct posix_acl *acl)
213 {
214         if ((acl == NULL) || (acl == ACL_NOT_CACHED))
215                 return;
216
217         if (atomic_dec_and_test(&acl->a_refcount)) {
218                 taskq_dispatch_delay(system_taskq, zpl_posix_acl_free, acl,
219                     TQ_SLEEP, ddi_get_lbolt() + 60*HZ);
220         }
221 }
222
223 static inline void
224 zpl_set_cached_acl(struct inode *ip, int type, struct posix_acl *newer) {
225 #ifdef HAVE_POSIX_ACL_CACHING
226         struct posix_acl *older = NULL;
227
228         spin_lock(&ip->i_lock);
229
230         if ((newer != ACL_NOT_CACHED) && (newer != NULL))
231                 posix_acl_dup(newer);
232
233         switch (type) {
234         case ACL_TYPE_ACCESS:
235                 older = ip->i_acl;
236                 rcu_assign_pointer(ip->i_acl, newer);
237                 break;
238         case ACL_TYPE_DEFAULT:
239                 older = ip->i_default_acl;
240                 rcu_assign_pointer(ip->i_default_acl, newer);
241                 break;
242         }
243
244         spin_unlock(&ip->i_lock);
245
246         zpl_posix_acl_release(older);
247 #endif /* HAVE_POSIX_ACL_CACHING */
248 }
249
250 static inline void
251 zpl_forget_cached_acl(struct inode *ip, int type) {
252         zpl_set_cached_acl(ip, type, (struct posix_acl *)ACL_NOT_CACHED);
253 }
254 #endif /* HAVE_POSIX_ACL_RELEASE */
255
256 /*
257  * 2.6.38 API change,
258  * The is_owner_or_cap() function was renamed to inode_owner_or_capable().
259  */
260 #ifdef HAVE_INODE_OWNER_OR_CAPABLE
261 #define zpl_inode_owner_or_capable(ip)          inode_owner_or_capable(ip)
262 #else
263 #define zpl_inode_owner_or_capable(ip)          is_owner_or_cap(ip)
264 #endif /* HAVE_INODE_OWNER_OR_CAPABLE */
265
266 #ifndef HAVE_POSIX_ACL_CHMOD
267 static inline int
268 posix_acl_chmod(struct posix_acl **acl, int flags, umode_t umode) {
269         struct posix_acl *oldacl = *acl;
270         mode_t mode = umode;
271         int error;
272
273         *acl = posix_acl_clone(*acl, flags);
274         zpl_posix_acl_release(oldacl);
275
276         if (!(*acl))
277                 return (-ENOMEM);
278
279         error = posix_acl_chmod_masq(*acl, mode);
280         if (error) {
281                 zpl_posix_acl_release(*acl);
282                 *acl = NULL;
283         }
284
285         return (error);
286 }
287
288 static inline int
289 posix_acl_create(struct posix_acl **acl, int flags, umode_t *umodep) {
290         struct posix_acl *oldacl = *acl;
291         mode_t mode = *umodep;
292         int error;
293
294         *acl = posix_acl_clone(*acl, flags);
295         zpl_posix_acl_release(oldacl);
296
297         if (!(*acl))
298                 return (-ENOMEM);
299
300         error = posix_acl_create_masq(*acl, &mode);
301         *umodep = mode;
302
303         if (error < 0) {
304                 zpl_posix_acl_release(*acl);
305                 *acl = NULL;
306         }
307
308         return (error);
309 }
310 #endif /* HAVE_POSIX_ACL_CHMOD */
311
312 #ifndef HAVE_CURRENT_UMASK
313 static inline int
314 current_umask(void)
315 {
316         return (current->fs->umask);
317 }
318 #endif /* HAVE_CURRENT_UMASK */
319
320 #ifdef HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T
321 typedef umode_t zpl_equivmode_t;
322 #else
323 typedef mode_t zpl_equivmode_t;
324 #endif /* HAVE_POSIX_ACL_EQUIV_MODE_UMODE_T */
325 #endif /* CONFIG_FS_POSIX_ACL */
326
327 #endif /* _ZFS_VFS_H */