blob: 260f757bbc5dd0dcea68e6fc6f0200cf2548e930 [file] [log] [blame]
David Chinner2a82b8b2007-07-11 11:09:12 +10001/*
2 * Copyright (c) 2006-2007 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#ifndef __XFS_FILESTREAM_H__
19#define __XFS_FILESTREAM_H__
20
21#ifdef __KERNEL__
22
23struct xfs_mount;
24struct xfs_inode;
25struct xfs_perag;
26struct xfs_bmalloca;
27
28#ifdef XFS_FILESTREAMS_TRACE
29#define XFS_FSTRM_KTRACE_INFO 1
30#define XFS_FSTRM_KTRACE_AGSCAN 2
31#define XFS_FSTRM_KTRACE_AGPICK1 3
32#define XFS_FSTRM_KTRACE_AGPICK2 4
33#define XFS_FSTRM_KTRACE_UPDATE 5
34#define XFS_FSTRM_KTRACE_FREE 6
35#define XFS_FSTRM_KTRACE_ITEM_LOOKUP 7
36#define XFS_FSTRM_KTRACE_ASSOCIATE 8
37#define XFS_FSTRM_KTRACE_MOVEAG 9
38#define XFS_FSTRM_KTRACE_ORPHAN 10
39
40#define XFS_FSTRM_KTRACE_SIZE 16384
41extern ktrace_t *xfs_filestreams_trace_buf;
42
43#endif
44
45/*
46 * Allocation group filestream associations are tracked with per-ag atomic
47 * counters. These counters allow _xfs_filestream_pick_ag() to tell whether a
48 * particular AG already has active filestreams associated with it. The mount
49 * point's m_peraglock is used to protect these counters from per-ag array
50 * re-allocation during a growfs operation. When xfs_growfs_data_private() is
51 * about to reallocate the array, it calls xfs_filestream_flush() with the
52 * m_peraglock held in write mode.
53 *
54 * Since xfs_mru_cache_flush() guarantees that all the free functions for all
55 * the cache elements have finished executing before it returns, it's safe for
56 * the free functions to use the atomic counters without m_peraglock protection.
57 * This allows the implementation of xfs_fstrm_free_func() to be agnostic about
58 * whether it was called with the m_peraglock held in read mode, write mode or
59 * not held at all. The race condition this addresses is the following:
60 *
61 * - The work queue scheduler fires and pulls a filestream directory cache
62 * element off the LRU end of the cache for deletion, then gets pre-empted.
63 * - A growfs operation grabs the m_peraglock in write mode, flushes all the
64 * remaining items from the cache and reallocates the mount point's per-ag
65 * array, resetting all the counters to zero.
66 * - The work queue thread resumes and calls the free function for the element
67 * it started cleaning up earlier. In the process it decrements the
68 * filestreams counter for an AG that now has no references.
69 *
70 * With a shrinkfs feature, the above scenario could panic the system.
71 *
72 * All other uses of the following macros should be protected by either the
73 * m_peraglock held in read mode, or the cache's internal locking exposed by the
74 * interval between a call to xfs_mru_cache_lookup() and a call to
75 * xfs_mru_cache_done(). In addition, the m_peraglock must be held in read mode
76 * when new elements are added to the cache.
77 *
78 * Combined, these locking rules ensure that no associations will ever exist in
79 * the cache that reference per-ag array elements that have since been
80 * reallocated.
81 */
Dave Chinner4196ac02010-01-11 11:47:42 +000082/*
83 * xfs_filestream_peek_ag is only used in tracing code
84 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +000085static inline int
David Chinner2a82b8b2007-07-11 11:09:12 +100086xfs_filestream_peek_ag(
87 xfs_mount_t *mp,
88 xfs_agnumber_t agno)
89{
Dave Chinner4196ac02010-01-11 11:47:42 +000090 struct xfs_perag *pag;
91 int ret;
92
93 pag = xfs_perag_get(mp, agno);
94 ret = atomic_read(&pag->pagf_fstrms);
95 xfs_perag_put(pag);
96 return ret;
David Chinner2a82b8b2007-07-11 11:09:12 +100097}
98
Christoph Hellwigb8f82a42009-11-14 16:17:22 +000099static inline int
David Chinner2a82b8b2007-07-11 11:09:12 +1000100xfs_filestream_get_ag(
101 xfs_mount_t *mp,
102 xfs_agnumber_t agno)
103{
Dave Chinner4196ac02010-01-11 11:47:42 +0000104 struct xfs_perag *pag;
105 int ret;
106
107 pag = xfs_perag_get(mp, agno);
108 ret = atomic_inc_return(&pag->pagf_fstrms);
109 xfs_perag_put(pag);
110 return ret;
David Chinner2a82b8b2007-07-11 11:09:12 +1000111}
112
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000113static inline int
David Chinner2a82b8b2007-07-11 11:09:12 +1000114xfs_filestream_put_ag(
115 xfs_mount_t *mp,
116 xfs_agnumber_t agno)
117{
Dave Chinner4196ac02010-01-11 11:47:42 +0000118 struct xfs_perag *pag;
119 int ret;
120
121 pag = xfs_perag_get(mp, agno);
122 ret = atomic_dec_return(&pag->pagf_fstrms);
123 xfs_perag_put(pag);
124 return ret;
David Chinner2a82b8b2007-07-11 11:09:12 +1000125}
126
127/* allocation selection flags */
128typedef enum xfs_fstrm_alloc {
129 XFS_PICK_USERDATA = 1,
130 XFS_PICK_LOWSPACE = 2,
131} xfs_fstrm_alloc_t;
132
133/* prototypes for filestream.c */
134int xfs_filestream_init(void);
135void xfs_filestream_uninit(void);
136int xfs_filestream_mount(struct xfs_mount *mp);
137void xfs_filestream_unmount(struct xfs_mount *mp);
David Chinner2a82b8b2007-07-11 11:09:12 +1000138xfs_agnumber_t xfs_filestream_lookup_ag(struct xfs_inode *ip);
139int xfs_filestream_associate(struct xfs_inode *dip, struct xfs_inode *ip);
140void xfs_filestream_deassociate(struct xfs_inode *ip);
141int xfs_filestream_new_ag(struct xfs_bmalloca *ap, xfs_agnumber_t *agp);
142
143
144/* filestreams for the inode? */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000145static inline int
David Chinner2a82b8b2007-07-11 11:09:12 +1000146xfs_inode_is_filestream(
147 struct xfs_inode *ip)
148{
149 return (ip->i_mount->m_flags & XFS_MOUNT_FILESTREAMS) ||
150 xfs_iflags_test(ip, XFS_IFILESTREAM) ||
151 (ip->i_d.di_flags & XFS_DIFLAG_FILESTREAM);
152}
153
154#endif /* __KERNEL__ */
155
156#endif /* __XFS_FILESTREAM_H__ */