2 * Copyright (C) 2005-2013 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/debugfs.h>
27 #error DEBUG_FS depends upon SYSFS
30 static struct dentry *dbgaufs;
31 static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
33 /* 20 is max digits length of ulong 64 */
40 * common function for all XINO files
42 static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
45 kfree(file->private_data);
49 static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
53 struct dbgaufs_arg *p;
56 p = kmalloc(sizeof(*p), GFP_NOFS);
62 file->private_data = p;
66 err = vfs_getattr(xf->f_vfsmnt, xf->f_dentry, &st);
70 (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
71 (long)file_count(xf), st.blocks, st.blksize,
74 p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
75 st.blocks, st.blksize,
77 AuDebugOn(p->n >= sizeof(p->a));
79 p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
88 static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
89 size_t count, loff_t *ppos)
91 struct dbgaufs_arg *p;
93 p = file->private_data;
94 return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
97 /* ---------------------------------------------------------------------- */
99 struct dbgaufs_plink_arg {
104 static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
107 free_page((unsigned long)file->private_data);
111 static int dbgaufs_plink_open(struct inode *inode, struct file *file)
114 unsigned long n, sum;
115 struct dbgaufs_plink_arg *p;
116 struct au_sbinfo *sbinfo;
117 struct super_block *sb;
118 struct au_sphlhead *sphl;
121 p = (void *)get_zeroed_page(GFP_NOFS);
126 sbinfo = inode->i_private;
128 si_noflush_read_lock(sb);
129 if (au_opt_test(au_mntflags(sb), PLINK)) {
130 limit = PAGE_SIZE - sizeof(p->n);
132 /* the number of buckets */
133 n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
138 for (i = 0, sphl = sbinfo->si_plink;
141 n = au_sphl_count(sphl);
144 n = snprintf(p->a + p->n, limit, "%lu ", n);
147 if (unlikely(limit <= 0))
150 p->a[p->n - 1] = '\n';
152 /* the sum of plinks */
153 n = snprintf(p->a + p->n, limit, "%lu\n", sum);
156 if (unlikely(limit <= 0))
159 #define str "1\n0\n0\n"
160 p->n = sizeof(str) - 1;
167 file->private_data = p;
168 goto out; /* success */
171 free_page((unsigned long)p);
176 static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
177 size_t count, loff_t *ppos)
179 struct dbgaufs_plink_arg *p;
181 p = file->private_data;
182 return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
185 static const struct file_operations dbgaufs_plink_fop = {
186 .owner = THIS_MODULE,
187 .open = dbgaufs_plink_open,
188 .release = dbgaufs_plink_release,
189 .read = dbgaufs_plink_read
192 /* ---------------------------------------------------------------------- */
194 static int dbgaufs_xib_open(struct inode *inode, struct file *file)
197 struct au_sbinfo *sbinfo;
198 struct super_block *sb;
200 sbinfo = inode->i_private;
202 si_noflush_read_lock(sb);
203 err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
208 static const struct file_operations dbgaufs_xib_fop = {
209 .owner = THIS_MODULE,
210 .open = dbgaufs_xib_open,
211 .release = dbgaufs_xi_release,
212 .read = dbgaufs_xi_read
215 /* ---------------------------------------------------------------------- */
217 #define DbgaufsXi_PREFIX "xi"
219 static int dbgaufs_xino_open(struct inode *inode, struct file *file)
223 struct au_sbinfo *sbinfo;
224 struct super_block *sb;
230 name = &file->f_dentry->d_name;
231 if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
232 || memcmp(name->name, DbgaufsXi_PREFIX,
233 sizeof(DbgaufsXi_PREFIX) - 1)))
235 err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
239 sbinfo = inode->i_private;
241 si_noflush_read_lock(sb);
242 if (l <= au_sbend(sb)) {
243 xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
244 err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
253 static const struct file_operations dbgaufs_xino_fop = {
254 .owner = THIS_MODULE,
255 .open = dbgaufs_xino_open,
256 .release = dbgaufs_xi_release,
257 .read = dbgaufs_xi_read
260 void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
263 struct au_branch *br;
264 struct au_xino_file *xi;
266 if (!au_sbi(sb)->si_dbgaufs)
270 for (; bindex <= bend; bindex++) {
271 br = au_sbr(sb, bindex);
273 debugfs_remove(xi->xi_dbgaufs);
274 xi->xi_dbgaufs = NULL;
278 void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
280 struct au_sbinfo *sbinfo;
281 struct dentry *parent;
282 struct au_branch *br;
283 struct au_xino_file *xi;
285 char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
288 parent = sbinfo->si_dbgaufs;
293 for (; bindex <= bend; bindex++) {
294 snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
295 br = au_sbr(sb, bindex);
297 AuDebugOn(xi->xi_dbgaufs);
298 xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
299 sbinfo, &dbgaufs_xino_fop);
300 /* ignore an error */
301 if (unlikely(!xi->xi_dbgaufs))
302 AuWarn1("failed %s under debugfs\n", name);
306 /* ---------------------------------------------------------------------- */
308 #ifdef CONFIG_AUFS_EXPORT
309 static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
312 struct au_sbinfo *sbinfo;
313 struct super_block *sb;
315 sbinfo = inode->i_private;
317 si_noflush_read_lock(sb);
318 err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
323 static const struct file_operations dbgaufs_xigen_fop = {
324 .owner = THIS_MODULE,
325 .open = dbgaufs_xigen_open,
326 .release = dbgaufs_xi_release,
327 .read = dbgaufs_xi_read
330 static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
335 * This function is a dynamic '__init' fucntion actually,
336 * so the tiny check for si_rwsem is unnecessary.
338 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
341 sbinfo->si_dbgaufs_xigen = debugfs_create_file
342 ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
344 if (sbinfo->si_dbgaufs_xigen)
350 static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
354 #endif /* CONFIG_AUFS_EXPORT */
356 /* ---------------------------------------------------------------------- */
358 void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
361 * This function is a dynamic '__init' fucntion actually,
362 * so the tiny check for si_rwsem is unnecessary.
364 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
366 debugfs_remove_recursive(sbinfo->si_dbgaufs);
367 sbinfo->si_dbgaufs = NULL;
368 kobject_put(&sbinfo->si_kobj);
371 int dbgaufs_si_init(struct au_sbinfo *sbinfo)
374 char name[SysaufsSiNameLen];
377 * This function is a dynamic '__init' fucntion actually,
378 * so the tiny check for si_rwsem is unnecessary.
380 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
384 AuErr1("/debug/aufs is uninitialized\n");
389 sysaufs_name(sbinfo, name);
390 sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
391 if (unlikely(!sbinfo->si_dbgaufs))
393 kobject_get(&sbinfo->si_kobj);
395 sbinfo->si_dbgaufs_xib = debugfs_create_file
396 ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
398 if (unlikely(!sbinfo->si_dbgaufs_xib))
401 sbinfo->si_dbgaufs_plink = debugfs_create_file
402 ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
404 if (unlikely(!sbinfo->si_dbgaufs_plink))
407 err = dbgaufs_xigen_init(sbinfo);
409 goto out; /* success */
412 dbgaufs_si_fin(sbinfo);
417 /* ---------------------------------------------------------------------- */
419 void dbgaufs_fin(void)
421 debugfs_remove(dbgaufs);
424 int __init dbgaufs_init(void)
429 dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);