1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/pathname.h>
#include <sys/uio.h>
#include <sys/tiuser.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <netinet/in.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/utsname.h>
#include <sys/bootconf.h>
#include <sys/modctl.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <vm/rm.h>
#include <sys/fs/cachefs_fs.h>
#define C_CACHE_VALID(SAVED_MTIME, NEW_MTIME) \
((SAVED_MTIME.tv_sec == NEW_MTIME.tv_sec) && \
(SAVED_MTIME.tv_nsec == NEW_MTIME.tv_nsec))
static time_t cachefs_gettime_cached_object(struct fscache *fscp,
struct cnode *cp, time_t mtime);
static int
c_strict_init_cached_object(fscache_t *fscp, cnode_t *cp, vattr_t *vap,
cred_t *cr)
{
int error;
cachefs_metadata_t *mdp = &cp->c_metadata;
ASSERT(cr);
ASSERT(MUTEX_HELD(&cp->c_statelock));
/* if attributes not passed in then get them */
if (vap == NULL) {
/* if not connected then cannot get attrs */
if ((fscp->fs_cdconnected != CFS_CD_CONNECTED) ||
(fscp->fs_backvfsp == NULL))
return (ETIMEDOUT);
/* get backvp if necessary */
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
return (error);
}
/* get the attributes */
cp->c_attr.va_mask = AT_ALL;
error = VOP_GETATTR(cp->c_backvp, &cp->c_attr, 0, cr, NULL);
if (error)
return (error);
} else {
/* copy passed in attributes into the cnode */
cp->c_attr = *vap;
}
/*
* Expire time is based on the number of seconds since
* the last change.
* (i.e. files that changed recently are likely to change soon)
*/
mdp->md_x_time.tv_nsec = 0;
mdp->md_x_time.tv_sec = cachefs_gettime_cached_object(fscp, cp,
cp->c_attr.va_mtime.tv_sec);
mdp->md_consttype = CFS_FS_CONST_STRICT;
cp->c_size = cp->c_attr.va_size;
cp->c_flags |= CN_UPDATED;
return (0);
}
static int
c_strict_check_cached_object(struct fscache *fscp, struct cnode *cp,
int verify_what, cred_t *cr)
{
struct vattr attrs;
int error = 0;
int fail = 0, backhit = 0;
cachefs_metadata_t *mdp = &cp->c_metadata;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("c_strict_check_cached_object: ENTER cp %p\n",
(void *)cp);
#endif
ASSERT(cr);
ASSERT(MUTEX_HELD(&cp->c_statelock));
if ((fscp->fs_cdconnected != CFS_CD_CONNECTED) ||
(fscp->fs_backvfsp == NULL))
goto out;
/*
* If backfs is NFSv4, do a getattr to update link count,
* all other attributes are not used, and the backfs is
* called on a getattr request.
*/
if (CFS_ISFS_BACKFS_NFSV4(fscp)) {
backhit = 1;
attrs.va_mask = AT_ALL;
error = VOP_GETATTR(cp->c_backvp, &attrs, 0, cr, NULL);
if (error)
goto out;
cp->c_attr = attrs;
goto out;
}
/* done if do not have to check and time has not expired */
if (((verify_what & C_BACK_CHECK) == 0) &&
(gethrestime_sec() < mdp->md_x_time.tv_sec) &&
((mdp->md_flags & MD_NEEDATTRS) == 0))
goto out;
/* get backvp if necessary */
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error)
goto out;
}
/*
* If the cnode is being populated, and we're not the populating
* thread, then block until the pop thread completes. If we are the
* pop thread, then we may come in here, but not to nuke the directory
* cnode at a critical juncture.
*/
again:
while ((cp->c_flags & CN_ASYNC_POP_WORKING) &&
(cp->c_popthrp != curthread)) {
cv_wait(&cp->c_popcv, &cp->c_statelock);
/*
* recheck backvp and connectivity - if backvp now null,
* something bad happened, so don't bother trying to 'get' it
*/
if ((cp->c_backvp == NULL) ||
(fscp->fs_cdconnected != CFS_CD_CONNECTED) ||
(fscp->fs_backvfsp == NULL)) {
if (cp->c_flags | CN_STALE) {
cp->c_flags |= CN_NOCACHE;
error = ESTALE;
}
goto out;
}
}
/* get the file attributes from the back fs */
attrs.va_mask = AT_ALL;
error = VOP_GETATTR(cp->c_backvp, &attrs, 0, cr, NULL);
backhit = 1;
if (error)
goto out;
/* if the mtime or size of the file has changed */
if ((!C_CACHE_VALID(mdp->md_vattr.va_mtime, attrs.va_mtime) ||
(cp->c_size != attrs.va_size)) &&
((mdp->md_flags & MD_NEEDATTRS) == 0)) {
fail = 1;
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_INVALIDATE)
printf("c_strict_check: invalidating %llu\n",
(u_longlong_t)cp->c_id.cid_fileno);
#endif
if (vn_has_cached_data(CTOV(cp))) {
mutex_exit(&cp->c_statelock);
error = cachefs_putpage_common(CTOV(cp),
(offset_t)0, 0, B_INVAL, cr);
mutex_enter(&cp->c_statelock);
if (CFS_TIMEOUT(fscp, error))
goto out;
error = 0;
/*
* if an async pop started while the lock was
* dropped, go back and try again
*/
if ((cp->c_flags & CN_ASYNC_POP_WORKING) &&
(cp->c_popthrp != curthread))
goto again;
}
/*
* We should properly handle the CN_NOCACHE flag here.
* In fact, we should remember that cachefs_inval_object()
* forcibly sets/unsets the flag, so we should keep a
* state of the flag over the call.
*/
if ((cp->c_flags & CN_NOCACHE) == 0)
cachefs_inval_object(cp);
else {
cachefs_inval_object(cp);
cp->c_flags |= CN_NOCACHE;
}
if ((CTOV(cp))->v_type == VREG) {
attrs.va_mask = AT_ALL;
error = VOP_GETATTR(cp->c_backvp, &attrs, 0, cr, NULL);
if (error)
goto out;
}
if (!vn_has_cached_data(CTOV(cp))) {
cp->c_size = attrs.va_size;
}
#ifdef CFSDEBUG
else {
CFS_DEBUG(CFSDEBUG_VOPS)
printf("c_strict_check: v_pages not null\n");
}
#endif
}
/* toss cached acl info if ctime changed */
if (!C_CACHE_VALID(mdp->md_vattr.va_ctime, attrs.va_ctime)) {
cachefs_purgeacl(cp);
}
cp->c_attr = attrs;
if (attrs.va_size > cp->c_size)
cp->c_size = attrs.va_size;
mdp->md_x_time.tv_sec =
cachefs_gettime_cached_object(fscp, cp, attrs.va_mtime.tv_sec);
mdp->md_flags &= ~MD_NEEDATTRS;
cachefs_cnode_setlocalstats(cp);
cp->c_flags |= CN_UPDATED;
out:
if (backhit != 0) {
if (fail != 0)
fscp->fs_stats.st_fails++;
else
fscp->fs_stats.st_passes++;
}
#ifdef CFSDEBUG
CFS_DEBUG(CFSDEBUG_VOPS)
printf("c_strict_check_cached_object: EXIT expires %lx\n",
(long)mdp->md_x_time.tv_sec);
#endif
return (error);
}
static void
c_strict_modify_cached_object(struct fscache *fscp, struct cnode *cp,
cred_t *cr)
{
struct vattr attrs;
int error = 0;
nlink_t nlink;
cachefs_metadata_t *mdp = &cp->c_metadata;
ASSERT(MUTEX_HELD(&cp->c_statelock));
ASSERT(fscp->fs_cdconnected == CFS_CD_CONNECTED);
ASSERT(fscp->fs_backvfsp);
/*
* Don't do a getattr if NFSv4, which maintains
* its attributes (and link count) by doing a call
* to CFSOP_CHECK_COBJECT() during vnode operations.
*/
if (CFS_ISFS_BACKFS_NFSV4(fscp))
goto out;
fscp->fs_stats.st_modifies++;
/* from now on, make sure we're using the server's idea of time */
mdp->md_flags &= ~(MD_LOCALCTIME | MD_LOCALMTIME);
mdp->md_flags |= MD_NEEDATTRS;
/* if in write-around mode, make sure file is nocached */
if (CFS_ISFS_WRITE_AROUND(fscp)) {
if ((cp->c_flags & CN_NOCACHE) == 0)
cachefs_nocache(cp);
/*
* If a directory, then defer getting the new attributes
* until requested. Might be a little bit faster this way.
*/
if (CTOV(cp)->v_type == VDIR)
goto out;
}
/* get the new mtime so the next call to check_cobject does not fail */
if (cp->c_backvp == NULL) {
error = cachefs_getbackvp(fscp, cp);
if (error) {
mdp->md_vattr.va_mtime.tv_sec = 0;
goto out;
}
}
attrs.va_mask = AT_ALL;
ASSERT(cp->c_backvp != NULL);
error = VOP_GETATTR(cp->c_backvp, &attrs, 0, cr, NULL);
if (error) {
mdp->md_vattr.va_mtime.tv_sec = 0;
goto out;
}
mdp->md_x_time.tv_sec =
cachefs_gettime_cached_object(fscp, cp, attrs.va_mtime.tv_sec);
nlink = cp->c_attr.va_nlink;
cp->c_attr = attrs;
cp->c_attr.va_nlink = nlink;
if ((attrs.va_size > cp->c_size) || !vn_has_cached_data(CTOV(cp)))
cp->c_size = attrs.va_size;
mdp->md_flags &= ~MD_NEEDATTRS;
cachefs_cnode_setlocalstats(cp);
out:
cp->c_flags |= CN_UPDATED;
}
/*ARGSUSED*/
static void
c_strict_invalidate_cached_object(struct fscache *fscp, struct cnode *cp,
cred_t *cr)
{
cachefs_metadata_t *mdp = &cp->c_metadata;
ASSERT(MUTEX_HELD(&cp->c_statelock));
mdp->md_vattr.va_mtime.tv_sec = 0;
mdp->md_flags |= MD_NEEDATTRS;
cp->c_flags |= CN_UPDATED;
}
/*ARGSUSED*/
static void
c_strict_convert_cached_object(struct fscache *fscp, struct cnode *cp,
cred_t *cr)
{
cachefs_metadata_t *mdp = &cp->c_metadata;
ASSERT(MUTEX_HELD(&cp->c_statelock));
mdp->md_flags |= MD_NEEDATTRS;
mdp->md_consttype = CFS_FS_CONST_STRICT;
cp->c_flags |= CN_UPDATED;
}
/*
* Returns the tod in secs when the consistency of the object should
* be checked.
*/
static time_t
cachefs_gettime_cached_object(struct fscache *fscp, struct cnode *cp,
time_t mtime)
{
time_t xsec;
time_t acmin, acmax;
time_t now;
/*
* Expire time is based on the number of seconds since the last change
* (i.e. files that changed recently are likely to change soon),
*/
if ((CTOV(cp))->v_type == VDIR) {
acmin = fscp->fs_acdirmin;
acmax = fscp->fs_acdirmax;
} else {
acmin = fscp->fs_acregmin;
acmax = fscp->fs_acregmax;
}
now = gethrestime_sec();
xsec = now - mtime;
xsec = MAX(xsec, acmin);
xsec = MIN(xsec, acmax);
xsec += now;
return (xsec);
}
struct cachefsops strictcfsops = {
c_strict_init_cached_object,
c_strict_check_cached_object,
c_strict_modify_cached_object,
c_strict_invalidate_cached_object,
c_strict_convert_cached_object
};
|