summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/tmpfs/tmp_tnode.c
blob: 13ea3569248af13a41b1725c6758f009290f39de (plain)
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
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2016 Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/user.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/stat.h>
#include <sys/mode.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/anon.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <sys/fs/tmp.h>
#include <sys/fs/tmpnode.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/swap.h>
#include <sys/vtrace.h>

/*
 * Reserve swap space for the size of the file.
 * Called before growing a file (i.e. ftruncate, write)
 * Returns 0 on success.
 */
int
tmp_resv(
	struct tmount *tm,
	struct tmpnode *tp,
	size_t delta,		/* size needed */
	int pagecreate)		/* call anon_resv if set */
{
	pgcnt_t pages = btopr(delta);
	size_t pbytes = ptob(pages);
	zone_t *zone;

	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
	ASSERT(tp->tn_type == VREG);

	/*
	 * pagecreate is set only if we actually need to call anon_resv to
	 * reserve an additional page of anonymous memory.  Since anon_resv
	 * always reserves a page at a time, it should only get called when we
	 * know we're growing the file into a new page or filling a hole. This
	 * is why we transform delta into a number of pages. However, because we
	 * track bytes and not pages, we convert that back to a number of bytes
	 * that we allocate against.
	 *
	 * Deny if trying to reserve more than tmpfs can allocate, the
	 * allocation causes an overflow, or the delta round up overflowed.
	 * Note, that btopr rounds up, so we need to catch the unsigned
	 * overflow. Note, rounding up when we are within a page of SIZE_MAX is
	 * done by adding a page, overflowing, which will then be rounded back
	 * to zero. Hence the following check.
	 */
	if (pages == 0 && delta != 0)
		return (1);

	zone = tm->tm_vfsp->vfs_zone;
	if (pagecreate && ((tm->tm_anonmem + pbytes > tm->tm_anonmax) ||
	    (tm->tm_anonmem + pbytes < tm->tm_anonmem) ||
	    (ptob(pages + tmpfs_minfree) <= pbytes) ||
	    (!anon_checkspace(ptob(pages + tmpfs_minfree), zone)) ||
	    (anon_try_resv_zone(delta, zone) == 0))) {
		return (1);
	}

	/*
	 * update statistics
	 */
	if (pagecreate) {
		mutex_enter(&tm->tm_contents);
		tm->tm_anonmem += pbytes;
		mutex_exit(&tm->tm_contents);

		TRACE_2(TR_FAC_VM, TR_ANON_TMPFS, "anon tmpfs:%p %lu",
		    tp, delta);
	}

	return (0);
}

/*
 * tmp_unresv - called when truncating a file
 * Only called if we're freeing at least pagesize bytes
 * because anon_unresv does a btopr(delta)
 */
static void
tmp_unresv(
	struct tmount *tm,
	struct tmpnode *tp,
	size_t delta)
{
	size_t pages, pbytes;

	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
	ASSERT(tp->tn_type == VREG);

	/*
	 * If this is true, we have a grevious overflow bug and some size
	 * accounting has been messed with as having an amount to truncate at
	 * this size would imply that all of memory was used for this file. No
	 * matter how small the kernel, it will always need at least one page.
	 */
	pages = btopr(delta);
	if (pages == 0 && delta != 0)
		panic("tmpfs unsigned overflow detected");
	pbytes = ptob(pages);

	anon_unresv_zone(delta, tm->tm_vfsp->vfs_zone);

	mutex_enter(&tm->tm_contents);
	ASSERT(tm->tm_anonmem > tm->tm_anonmem - pbytes);
	tm->tm_anonmem -= pbytes;
	mutex_exit(&tm->tm_contents);

	TRACE_2(TR_FAC_VM, TR_ANON_TMPFS, "anon tmpfs:%p %lu", tp, delta);
}

#define	TMP_INIT_SZ	128

/*
 * Grow the anon pointer array to cover 'newsize' bytes plus slack.
 */
void
tmpnode_growmap(struct tmpnode *tp, ulong_t newsize)
{
	pgcnt_t np = btopr(newsize);

	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
	ASSERT(RW_WRITE_HELD(&tp->tn_contents));
	ASSERT(tp->tn_type == VREG);

	if (tp->tn_asize >= np)
		return;

	if (newsize > MAXOFF_T)
		np = btopr((u_offset_t)MAXOFF_T);

	if (tp->tn_anon == NULL) {
		tp->tn_anon = anon_create(MAX(np, TMP_INIT_SZ), ANON_SLEEP);
		tp->tn_asize = tp->tn_anon->size;
		return;
	}

	tp->tn_asize = anon_grow(tp->tn_anon, NULL, tp->tn_asize,
	    np - tp->tn_asize, ANON_SLEEP);
	ASSERT(tp->tn_asize >= np);
}

/*
 * This is used to clean up a tmpnode that hasn't made it out the door. In other
 * words, we allocated it and did a tmpnode_init; however, before it could get
 * fully inserted into a directory, bad things happened and it failed.
 */
void
tmpnode_cleanup(struct tmpnode *tp)
{
	rw_enter(&tp->tn_rwlock, RW_WRITER);
	if ((tp->tn_type) == VDIR) {
		tdirtrunc(tp);
	}
	mutex_enter(&tp->tn_tlock);
	tp->tn_nlink = 0;
	mutex_exit(&tp->tn_tlock);
	gethrestime(&tp->tn_ctime);
	rw_exit(&tp->tn_rwlock);
	tmpnode_rele(tp);
}

/*
 * Initialize a tmpnode and add it to file list under mount point.
 */
void
tmpnode_init(struct tmount *tm, struct tmpnode *t, vattr_t *vap, cred_t *cred)
{
	struct vnode *vp;
	timestruc_t now;

	ASSERT(vap != NULL);

	rw_init(&t->tn_rwlock, NULL, RW_DEFAULT, NULL);
	mutex_init(&t->tn_tlock, NULL, MUTEX_DEFAULT, NULL);
	t->tn_mode = MAKEIMODE(vap->va_type, vap->va_mode);
	t->tn_mask = 0;
	t->tn_type = vap->va_type;
	t->tn_nodeid = (ino64_t)(uint32_t)((uintptr_t)t >> 3);
	t->tn_nlink = 1;
	t->tn_size = 0;

	if (cred == NULL) {
		t->tn_uid = vap->va_uid;
		t->tn_gid = vap->va_gid;
	} else {
		t->tn_uid = crgetuid(cred);
		t->tn_gid = crgetgid(cred);
	}

	t->tn_fsid = tm->tm_dev;
	t->tn_rdev = vap->va_rdev;
	t->tn_blksize = PAGESIZE;
	t->tn_nblocks = 0;
	gethrestime(&now);
	t->tn_atime = now;
	t->tn_mtime = now;
	t->tn_ctime = now;
	t->tn_seq = 0;
	t->tn_dir = NULL;

	t->tn_vnode = vn_alloc(KM_SLEEP);
	vp = TNTOV(t);
	vn_setops(vp, tmp_vnodeops);
	vp->v_vfsp = tm->tm_vfsp;
	vp->v_type = vap->va_type;
	vp->v_rdev = vap->va_rdev;
	vp->v_data = (caddr_t)t;
	mutex_enter(&tm->tm_contents);
	/*
	 * Increment the pseudo generation number for this tmpnode.
	 * Since tmpnodes are allocated and freed, there really is no
	 * particular generation number for a new tmpnode.  Just fake it
	 * by using a counter in each file system.
	 */
	t->tn_gen = tm->tm_gen++;

	/*
	 * Add new tmpnode to end of linked list of tmpnodes for this tmpfs
	 * Root directory is handled specially in tmp_mount.
	 */
	if (tm->tm_rootnode != (struct tmpnode *)NULL) {
		t->tn_forw = NULL;
		t->tn_back = tm->tm_rootnode->tn_back;
		t->tn_back->tn_forw = tm->tm_rootnode->tn_back = t;
	}
	mutex_exit(&tm->tm_contents);
	vn_exists(vp);
}

/*
 * tmpnode_trunc - set length of tmpnode and deal with resources
 */
int
tmpnode_trunc(
	struct tmount *tm,
	struct tmpnode *tp,
	ulong_t newsize)
{
	size_t oldsize = tp->tn_size;
	size_t delta;
	timestruc_t now;
	int error = 0;

	ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
	ASSERT(RW_WRITE_HELD(&tp->tn_contents));

	if (newsize == oldsize) {
		/* Required by POSIX */
		goto stamp_out;
	}

	switch (tp->tn_type) {
	case VREG:
		/* Growing the file */
		if (newsize > oldsize) {
			delta = P2ROUNDUP(newsize, PAGESIZE) -
			    P2ROUNDUP(oldsize, PAGESIZE);
			/*
			 * Grow the size of the anon array to the new size
			 * Reserve the space for the growth here.
			 * We do it this way for now because this is how
			 * tmpfs used to do it, and this way the reserved
			 * space is alway equal to the file size.
			 * Alternatively, we could wait to reserve space 'til
			 * someone tries to store into one of the newly
			 * trunc'ed up pages. This would give us behavior
			 * identical to ufs; i.e., you could fail a
			 * fault on storing into a holey region of a file
			 * if there is no space in the filesystem to fill
			 * the hole at that time.
			 */
			/*
			 * tmp_resv calls anon_resv only if we're extending
			 * the file into a new page
			 */
			if (tmp_resv(tm, tp, delta,
			    (btopr(newsize) != btopr(oldsize)))) {
				error = ENOSPC;
				goto out;
			}
			tmpnode_growmap(tp, newsize);
			tp->tn_size = newsize;
			break;
		}

		/* Free anon pages if shrinking file over page boundary. */
		if (btopr(newsize) != btopr(oldsize)) {
			pgcnt_t freed;
			delta = P2ROUNDUP(oldsize, PAGESIZE) -
			    P2ROUNDUP(newsize, PAGESIZE);
			freed = anon_pages(tp->tn_anon, btopr(newsize),
			    btopr(delta));
			tp->tn_nblocks -= freed;
			anon_free(tp->tn_anon, btopr(newsize), delta);
			tmp_unresv(tm, tp, delta);
		}

		/*
		 * Update the file size now to reflect the pages we just
		 * blew away as we're about to drop the
		 * contents lock to zero the partial page (which could
		 * re-enter tmpfs via getpage and try to reacquire the lock)
		 * Once we drop the lock, faulters can fill in holes in
		 * the file and if we haven't updated the size they
		 * may fill in holes that are beyond EOF, which will then
		 * never get cleared.
		 */
		tp->tn_size = newsize;

		/* Zero new size of file to page boundary. */
		if (anon_get_ptr(tp->tn_anon, btop(newsize)) != NULL) {
			size_t zlen;

			zlen = PAGESIZE - ((ulong_t)newsize & PAGEOFFSET);
			rw_exit(&tp->tn_contents);
			pvn_vpzero(TNTOV(tp), (u_offset_t)newsize, zlen);
			rw_enter(&tp->tn_contents, RW_WRITER);
		}

		if (newsize == 0) {
			/* Delete anon array for tmpnode */
			ASSERT(tp->tn_nblocks == 0);
			ASSERT(anon_get_ptr(tp->tn_anon, 0) == NULL);
			ASSERT(!vn_has_cached_data(TNTOV(tp)));

			anon_release(tp->tn_anon, tp->tn_asize);
			tp->tn_anon = NULL;
			tp->tn_asize = 0;
		}
		break;
	case VLNK:
		/*
		 * Don't do anything here
		 * tmp_inactive frees the memory
		 */
		if (newsize != 0)
			error = EINVAL;
		goto out;
	case VDIR:
		/*
		 * Remove all the directory entries under this directory.
		 */
		if (newsize != 0) {
			error = EINVAL;
			goto out;
		}
		tdirtrunc(tp);
		ASSERT(tp->tn_nlink == 0);
		break;
	default:
		goto out;
	}

stamp_out:
	gethrestime(&now);
	tp->tn_mtime = now;
	tp->tn_ctime = now;
out:
	/*
	 * tmpnode_trunc() cannot fail when newsize == 0.
	 */
	ASSERT(error == 0 || newsize != 0);
	return (error);
}