summaryrefslogtreecommitdiff
path: root/fs/aufs/fhsm.c
blob: 7ca4d1759bba92465b004c25435567cd35723851 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (C) 2011-2015 Junjiro R. Okajima
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * File-based Hierarchy Storage Management
 */

#include <linux/anon_inodes.h>
#include <linux/poll.h>
#include <linux/seq_file.h>
#include <linux/statfs.h>
#include "aufs.h"

static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
{
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	SiMustAnyLock(sb);

	sbinfo = au_sbi(sb);
	fhsm = &sbinfo->si_fhsm;
	AuDebugOn(!fhsm);
	return fhsm->fhsm_bottom;
}

void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
{
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	SiMustWriteLock(sb);

	sbinfo = au_sbi(sb);
	fhsm = &sbinfo->si_fhsm;
	AuDebugOn(!fhsm);
	fhsm->fhsm_bottom = bindex;
}

/* ---------------------------------------------------------------------- */

static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
{
	struct au_br_fhsm *bf;

	bf = br->br_fhsm;
	MtxMustLock(&bf->bf_lock);

	return !bf->bf_readable
		|| time_after(jiffies,
			      bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
}

/* ---------------------------------------------------------------------- */

static void au_fhsm_notify(struct super_block *sb, int val)
{
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	SiMustAnyLock(sb);

	sbinfo = au_sbi(sb);
	fhsm = &sbinfo->si_fhsm;
	if (au_fhsm_pid(fhsm)
	    && atomic_read(&fhsm->fhsm_readable) != -1) {
		atomic_set(&fhsm->fhsm_readable, val);
		if (val)
			wake_up(&fhsm->fhsm_wqh);
	}
}

static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
			struct aufs_stfs *rstfs, int do_lock, int do_notify)
{
	int err;
	struct au_branch *br;
	struct au_br_fhsm *bf;

	br = au_sbr(sb, bindex);
	AuDebugOn(au_br_rdonly(br));
	bf = br->br_fhsm;
	AuDebugOn(!bf);

	if (do_lock)
		mutex_lock(&bf->bf_lock);
	else
		MtxMustLock(&bf->bf_lock);

	/* sb->s_root for NFS is unreliable */
	err = au_br_stfs(br, &bf->bf_stfs);
	if (unlikely(err)) {
		AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
		goto out;
	}

	bf->bf_jiffy = jiffies;
	bf->bf_readable = 1;
	if (do_notify)
		au_fhsm_notify(sb, /*val*/1);
	if (rstfs)
		*rstfs = bf->bf_stfs;

out:
	if (do_lock)
		mutex_unlock(&bf->bf_lock);
	au_fhsm_notify(sb, /*val*/1);

	return err;
}

void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
{
	int err;
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;
	struct au_branch *br;
	struct au_br_fhsm *bf;

	AuDbg("b%d, force %d\n", bindex, force);
	SiMustAnyLock(sb);

	sbinfo = au_sbi(sb);
	fhsm = &sbinfo->si_fhsm;
	if (!au_ftest_si(sbinfo, FHSM)
	    || fhsm->fhsm_bottom == bindex)
		return;

	br = au_sbr(sb, bindex);
	bf = br->br_fhsm;
	AuDebugOn(!bf);
	mutex_lock(&bf->bf_lock);
	if (force
	    || au_fhsm_pid(fhsm)
	    || au_fhsm_test_jiffy(sbinfo, br))
		err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
				  /*do_notify*/1);
	mutex_unlock(&bf->bf_lock);
}

void au_fhsm_wrote_all(struct super_block *sb, int force)
{
	aufs_bindex_t bindex, bend;
	struct au_branch *br;

	/* exclude the bottom */
	bend = au_fhsm_bottom(sb);
	for (bindex = 0; bindex < bend; bindex++) {
		br = au_sbr(sb, bindex);
		if (au_br_fhsm(br->br_perm))
			au_fhsm_wrote(sb, bindex, force);
	}
}

/* ---------------------------------------------------------------------- */

static unsigned int au_fhsm_poll(struct file *file,
				 struct poll_table_struct *wait)
{
	unsigned int mask;
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	mask = 0;
	sbinfo = file->private_data;
	fhsm = &sbinfo->si_fhsm;
	poll_wait(file, &fhsm->fhsm_wqh, wait);
	if (atomic_read(&fhsm->fhsm_readable))
		mask = POLLIN /* | POLLRDNORM */;

	AuTraceErr((int)mask);
	return mask;
}

static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
			      struct aufs_stfs *stfs, __s16 brid)
{
	int err;

	err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
	if (!err)
		err = __put_user(brid, &stbr->brid);
	if (unlikely(err))
		err = -EFAULT;

	return err;
}

static ssize_t au_fhsm_do_read(struct super_block *sb,
			       struct aufs_stbr __user *stbr, size_t count)
{
	ssize_t err;
	int nstbr;
	aufs_bindex_t bindex, bend;
	struct au_branch *br;
	struct au_br_fhsm *bf;

	/* except the bottom branch */
	err = 0;
	nstbr = 0;
	bend = au_fhsm_bottom(sb);
	for (bindex = 0; !err && bindex < bend; bindex++) {
		br = au_sbr(sb, bindex);
		if (!au_br_fhsm(br->br_perm))
			continue;

		bf = br->br_fhsm;
		mutex_lock(&bf->bf_lock);
		if (bf->bf_readable) {
			err = -EFAULT;
			if (count >= sizeof(*stbr))
				err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
							  br->br_id);
			if (!err) {
				bf->bf_readable = 0;
				count -= sizeof(*stbr);
				nstbr++;
			}
		}
		mutex_unlock(&bf->bf_lock);
	}
	if (!err)
		err = sizeof(*stbr) * nstbr;

	return err;
}

static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
			   loff_t *pos)
{
	ssize_t err;
	int readable;
	aufs_bindex_t nfhsm, bindex, bend;
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;
	struct au_branch *br;
	struct super_block *sb;

	err = 0;
	sbinfo = file->private_data;
	fhsm = &sbinfo->si_fhsm;
need_data:
	spin_lock_irq(&fhsm->fhsm_wqh.lock);
	if (!atomic_read(&fhsm->fhsm_readable)) {
		if (vfsub_file_flags(file) & O_NONBLOCK)
			err = -EAGAIN;
		else
			err = wait_event_interruptible_locked_irq
				(fhsm->fhsm_wqh,
				 atomic_read(&fhsm->fhsm_readable));
	}
	spin_unlock_irq(&fhsm->fhsm_wqh.lock);
	if (unlikely(err))
		goto out;

	/* sb may already be dead */
	au_rw_read_lock(&sbinfo->si_rwsem);
	readable = atomic_read(&fhsm->fhsm_readable);
	if (readable > 0) {
		sb = sbinfo->si_sb;
		AuDebugOn(!sb);
		/* exclude the bottom branch */
		nfhsm = 0;
		bend = au_fhsm_bottom(sb);
		for (bindex = 0; bindex < bend; bindex++) {
			br = au_sbr(sb, bindex);
			if (au_br_fhsm(br->br_perm))
				nfhsm++;
		}
		err = -EMSGSIZE;
		if (nfhsm * sizeof(struct aufs_stbr) <= count) {
			atomic_set(&fhsm->fhsm_readable, 0);
			err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
					     count);
		}
	}
	au_rw_read_unlock(&sbinfo->si_rwsem);
	if (!readable)
		goto need_data;

out:
	return err;
}

static int au_fhsm_release(struct inode *inode, struct file *file)
{
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	/* sb may already be dead */
	sbinfo = file->private_data;
	fhsm = &sbinfo->si_fhsm;
	spin_lock(&fhsm->fhsm_spin);
	fhsm->fhsm_pid = 0;
	spin_unlock(&fhsm->fhsm_spin);
	kobject_put(&sbinfo->si_kobj);

	return 0;
}

static const struct file_operations au_fhsm_fops = {
	.owner		= THIS_MODULE,
	.llseek		= noop_llseek,
	.read		= au_fhsm_read,
	.poll		= au_fhsm_poll,
	.release	= au_fhsm_release
};

int au_fhsm_fd(struct super_block *sb, int oflags)
{
	int err, fd;
	struct au_sbinfo *sbinfo;
	struct au_fhsm *fhsm;

	err = -EPERM;
	if (unlikely(!capable(CAP_SYS_ADMIN)))
		goto out;

	err = -EINVAL;
	if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
		goto out;

	err = 0;
	sbinfo = au_sbi(sb);
	fhsm = &sbinfo->si_fhsm;
	spin_lock(&fhsm->fhsm_spin);
	if (!fhsm->fhsm_pid)
		fhsm->fhsm_pid = current->pid;
	else
		err = -EBUSY;
	spin_unlock(&fhsm->fhsm_spin);
	if (unlikely(err))
		goto out;

	oflags |= O_RDONLY;
	/* oflags |= FMODE_NONOTIFY; */
	fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
	err = fd;
	if (unlikely(fd < 0))
		goto out_pid;

	/* succeed reglardless 'fhsm' status */
	kobject_get(&sbinfo->si_kobj);
	si_noflush_read_lock(sb);
	if (au_ftest_si(sbinfo, FHSM))
		au_fhsm_wrote_all(sb, /*force*/0);
	si_read_unlock(sb);
	goto out; /* success */

out_pid:
	spin_lock(&fhsm->fhsm_spin);
	fhsm->fhsm_pid = 0;
	spin_unlock(&fhsm->fhsm_spin);
out:
	AuTraceErr(err);
	return err;
}

/* ---------------------------------------------------------------------- */

int au_fhsm_br_alloc(struct au_branch *br)
{
	int err;

	err = 0;
	br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
	if (br->br_fhsm)
		au_br_fhsm_init(br->br_fhsm);
	else
		err = -ENOMEM;

	return err;
}

/* ---------------------------------------------------------------------- */

void au_fhsm_fin(struct super_block *sb)
{
	au_fhsm_notify(sb, /*val*/-1);
}

void au_fhsm_init(struct au_sbinfo *sbinfo)
{
	struct au_fhsm *fhsm;

	fhsm = &sbinfo->si_fhsm;
	spin_lock_init(&fhsm->fhsm_spin);
	init_waitqueue_head(&fhsm->fhsm_wqh);
	atomic_set(&fhsm->fhsm_readable, 0);
	fhsm->fhsm_expire
		= msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
	fhsm->fhsm_bottom = -1;
}

void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
{
	sbinfo->si_fhsm.fhsm_expire
		= msecs_to_jiffies(sec * MSEC_PER_SEC);
}

void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
{
	unsigned int u;

	if (!au_ftest_si(sbinfo, FHSM))
		return;

	u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
	if (u != AUFS_FHSM_CACHE_DEF_SEC)
		seq_printf(seq, ",fhsm_sec=%u", u);
}