Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * proc/fs/generic.c --- generic routines for the proc-fs | |
3 | * | |
4 | * This file contains generic proc-fs routines for handling | |
5 | * directories and files. | |
6 | * | |
7 | * Copyright (C) 1991, 1992 Linus Torvalds. | |
8 | * Copyright (C) 1997 Theodore Ts'o | |
9 | */ | |
10 | ||
11 | #include <linux/errno.h> | |
12 | #include <linux/time.h> | |
13 | #include <linux/proc_fs.h> | |
14 | #include <linux/stat.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/mount.h> | |
1da177e4 LT |
17 | #include <linux/init.h> |
18 | #include <linux/idr.h> | |
19 | #include <linux/namei.h> | |
20 | #include <linux/bitops.h> | |
64a07bd8 | 21 | #include <linux/spinlock.h> |
786d7e16 | 22 | #include <linux/completion.h> |
1da177e4 LT |
23 | #include <asm/uaccess.h> |
24 | ||
fee781e6 AB |
25 | #include "internal.h" |
26 | ||
64a07bd8 SR |
27 | DEFINE_SPINLOCK(proc_subdir_lock); |
28 | ||
77b14db5 | 29 | static int proc_match(int len, const char *name, struct proc_dir_entry *de) |
1da177e4 LT |
30 | { |
31 | if (de->namelen != len) | |
32 | return 0; | |
33 | return !memcmp(name, de->name, len); | |
34 | } | |
35 | ||
1da177e4 LT |
36 | /* buffer size is one page but our output routines use some slack for overruns */ |
37 | #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024) | |
38 | ||
39 | static ssize_t | |
3dec7f59 | 40 | __proc_file_read(struct file *file, char __user *buf, size_t nbytes, |
1da177e4 LT |
41 | loff_t *ppos) |
42 | { | |
2fddfeef | 43 | struct inode * inode = file->f_path.dentry->d_inode; |
1da177e4 LT |
44 | char *page; |
45 | ssize_t retval=0; | |
46 | int eof=0; | |
47 | ssize_t n, count; | |
48 | char *start; | |
49 | struct proc_dir_entry * dp; | |
8b90db0d LT |
50 | unsigned long long pos; |
51 | ||
52 | /* | |
53 | * Gaah, please just use "seq_file" instead. The legacy /proc | |
54 | * interfaces cut loff_t down to off_t for reads, and ignore | |
55 | * the offset entirely for writes.. | |
56 | */ | |
57 | pos = *ppos; | |
58 | if (pos > MAX_NON_LFS) | |
59 | return 0; | |
60 | if (nbytes > MAX_NON_LFS - pos) | |
61 | nbytes = MAX_NON_LFS - pos; | |
1da177e4 LT |
62 | |
63 | dp = PDE(inode); | |
e12ba74d | 64 | if (!(page = (char*) __get_free_page(GFP_TEMPORARY))) |
1da177e4 LT |
65 | return -ENOMEM; |
66 | ||
67 | while ((nbytes > 0) && !eof) { | |
68 | count = min_t(size_t, PROC_BLOCK_SIZE, nbytes); | |
69 | ||
70 | start = NULL; | |
8731f14d | 71 | if (dp->read_proc) { |
1da177e4 LT |
72 | /* |
73 | * How to be a proc read function | |
74 | * ------------------------------ | |
75 | * Prototype: | |
76 | * int f(char *buffer, char **start, off_t offset, | |
77 | * int count, int *peof, void *dat) | |
78 | * | |
79 | * Assume that the buffer is "count" bytes in size. | |
80 | * | |
81 | * If you know you have supplied all the data you | |
82 | * have, set *peof. | |
83 | * | |
84 | * You have three ways to return data: | |
85 | * 0) Leave *start = NULL. (This is the default.) | |
86 | * Put the data of the requested offset at that | |
87 | * offset within the buffer. Return the number (n) | |
88 | * of bytes there are from the beginning of the | |
89 | * buffer up to the last byte of data. If the | |
90 | * number of supplied bytes (= n - offset) is | |
91 | * greater than zero and you didn't signal eof | |
92 | * and the reader is prepared to take more data | |
93 | * you will be called again with the requested | |
94 | * offset advanced by the number of bytes | |
95 | * absorbed. This interface is useful for files | |
96 | * no larger than the buffer. | |
97 | * 1) Set *start = an unsigned long value less than | |
98 | * the buffer address but greater than zero. | |
99 | * Put the data of the requested offset at the | |
100 | * beginning of the buffer. Return the number of | |
101 | * bytes of data placed there. If this number is | |
102 | * greater than zero and you didn't signal eof | |
103 | * and the reader is prepared to take more data | |
104 | * you will be called again with the requested | |
105 | * offset advanced by *start. This interface is | |
106 | * useful when you have a large file consisting | |
107 | * of a series of blocks which you want to count | |
108 | * and return as wholes. | |
109 | * (Hack by Paul.Russell@rustcorp.com.au) | |
110 | * 2) Set *start = an address within the buffer. | |
111 | * Put the data of the requested offset at *start. | |
112 | * Return the number of bytes of data placed there. | |
113 | * If this number is greater than zero and you | |
114 | * didn't signal eof and the reader is prepared to | |
115 | * take more data you will be called again with the | |
116 | * requested offset advanced by the number of bytes | |
117 | * absorbed. | |
118 | */ | |
119 | n = dp->read_proc(page, &start, *ppos, | |
120 | count, &eof, dp->data); | |
121 | } else | |
122 | break; | |
123 | ||
124 | if (n == 0) /* end of file */ | |
125 | break; | |
126 | if (n < 0) { /* error */ | |
127 | if (retval == 0) | |
128 | retval = n; | |
129 | break; | |
130 | } | |
131 | ||
132 | if (start == NULL) { | |
133 | if (n > PAGE_SIZE) { | |
134 | printk(KERN_ERR | |
135 | "proc_file_read: Apparent buffer overflow!\n"); | |
136 | n = PAGE_SIZE; | |
137 | } | |
138 | n -= *ppos; | |
139 | if (n <= 0) | |
140 | break; | |
141 | if (n > count) | |
142 | n = count; | |
143 | start = page + *ppos; | |
144 | } else if (start < page) { | |
145 | if (n > PAGE_SIZE) { | |
146 | printk(KERN_ERR | |
147 | "proc_file_read: Apparent buffer overflow!\n"); | |
148 | n = PAGE_SIZE; | |
149 | } | |
150 | if (n > count) { | |
151 | /* | |
152 | * Don't reduce n because doing so might | |
153 | * cut off part of a data block. | |
154 | */ | |
155 | printk(KERN_WARNING | |
156 | "proc_file_read: Read count exceeded\n"); | |
157 | } | |
158 | } else /* start >= page */ { | |
159 | unsigned long startoff = (unsigned long)(start - page); | |
160 | if (n > (PAGE_SIZE - startoff)) { | |
161 | printk(KERN_ERR | |
162 | "proc_file_read: Apparent buffer overflow!\n"); | |
163 | n = PAGE_SIZE - startoff; | |
164 | } | |
165 | if (n > count) | |
166 | n = count; | |
167 | } | |
168 | ||
169 | n -= copy_to_user(buf, start < page ? page : start, n); | |
170 | if (n == 0) { | |
171 | if (retval == 0) | |
172 | retval = -EFAULT; | |
173 | break; | |
174 | } | |
175 | ||
176 | *ppos += start < page ? (unsigned long)start : n; | |
177 | nbytes -= n; | |
178 | buf += n; | |
179 | retval += n; | |
180 | } | |
181 | free_page((unsigned long) page); | |
182 | return retval; | |
183 | } | |
184 | ||
3dec7f59 AD |
185 | static ssize_t |
186 | proc_file_read(struct file *file, char __user *buf, size_t nbytes, | |
187 | loff_t *ppos) | |
188 | { | |
189 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
190 | ssize_t rv = -EIO; | |
191 | ||
192 | spin_lock(&pde->pde_unload_lock); | |
193 | if (!pde->proc_fops) { | |
194 | spin_unlock(&pde->pde_unload_lock); | |
195 | return rv; | |
196 | } | |
197 | pde->pde_users++; | |
198 | spin_unlock(&pde->pde_unload_lock); | |
199 | ||
200 | rv = __proc_file_read(file, buf, nbytes, ppos); | |
201 | ||
202 | pde_users_dec(pde); | |
203 | return rv; | |
204 | } | |
205 | ||
1da177e4 LT |
206 | static ssize_t |
207 | proc_file_write(struct file *file, const char __user *buffer, | |
208 | size_t count, loff_t *ppos) | |
209 | { | |
3dec7f59 AD |
210 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); |
211 | ssize_t rv = -EIO; | |
212 | ||
213 | if (pde->write_proc) { | |
214 | spin_lock(&pde->pde_unload_lock); | |
215 | if (!pde->proc_fops) { | |
216 | spin_unlock(&pde->pde_unload_lock); | |
217 | return rv; | |
218 | } | |
219 | pde->pde_users++; | |
220 | spin_unlock(&pde->pde_unload_lock); | |
1da177e4 | 221 | |
3dec7f59 AD |
222 | /* FIXME: does this routine need ppos? probably... */ |
223 | rv = pde->write_proc(file, buffer, count, pde->data); | |
224 | pde_users_dec(pde); | |
225 | } | |
226 | return rv; | |
1da177e4 LT |
227 | } |
228 | ||
229 | ||
230 | static loff_t | |
231 | proc_file_lseek(struct file *file, loff_t offset, int orig) | |
232 | { | |
8b90db0d LT |
233 | loff_t retval = -EINVAL; |
234 | switch (orig) { | |
235 | case 1: | |
236 | offset += file->f_pos; | |
237 | /* fallthrough */ | |
238 | case 0: | |
239 | if (offset < 0 || offset > MAX_NON_LFS) | |
240 | break; | |
241 | file->f_pos = retval = offset; | |
242 | } | |
243 | return retval; | |
1da177e4 LT |
244 | } |
245 | ||
76df0c25 AD |
246 | static const struct file_operations proc_file_operations = { |
247 | .llseek = proc_file_lseek, | |
248 | .read = proc_file_read, | |
249 | .write = proc_file_write, | |
250 | }; | |
251 | ||
1da177e4 LT |
252 | static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) |
253 | { | |
254 | struct inode *inode = dentry->d_inode; | |
255 | struct proc_dir_entry *de = PDE(inode); | |
256 | int error; | |
257 | ||
258 | error = inode_change_ok(inode, iattr); | |
259 | if (error) | |
260 | goto out; | |
261 | ||
262 | error = inode_setattr(inode, iattr); | |
263 | if (error) | |
264 | goto out; | |
265 | ||
266 | de->uid = inode->i_uid; | |
267 | de->gid = inode->i_gid; | |
268 | de->mode = inode->i_mode; | |
269 | out: | |
270 | return error; | |
271 | } | |
272 | ||
2b579bee MS |
273 | static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry, |
274 | struct kstat *stat) | |
275 | { | |
276 | struct inode *inode = dentry->d_inode; | |
277 | struct proc_dir_entry *de = PROC_I(inode)->pde; | |
278 | if (de && de->nlink) | |
279 | inode->i_nlink = de->nlink; | |
280 | ||
281 | generic_fillattr(inode, stat); | |
282 | return 0; | |
283 | } | |
284 | ||
c5ef1c42 | 285 | static const struct inode_operations proc_file_inode_operations = { |
1da177e4 LT |
286 | .setattr = proc_notify_change, |
287 | }; | |
288 | ||
289 | /* | |
290 | * This function parses a name such as "tty/driver/serial", and | |
291 | * returns the struct proc_dir_entry for "/proc/tty/driver", and | |
292 | * returns "serial" in residual. | |
293 | */ | |
294 | static int xlate_proc_name(const char *name, | |
295 | struct proc_dir_entry **ret, const char **residual) | |
296 | { | |
297 | const char *cp = name, *next; | |
298 | struct proc_dir_entry *de; | |
299 | int len; | |
64a07bd8 | 300 | int rtn = 0; |
1da177e4 | 301 | |
7cee4e00 AD |
302 | de = *ret; |
303 | if (!de) | |
304 | de = &proc_root; | |
305 | ||
64a07bd8 | 306 | spin_lock(&proc_subdir_lock); |
1da177e4 LT |
307 | while (1) { |
308 | next = strchr(cp, '/'); | |
309 | if (!next) | |
310 | break; | |
311 | ||
312 | len = next - cp; | |
313 | for (de = de->subdir; de ; de = de->next) { | |
314 | if (proc_match(len, cp, de)) | |
315 | break; | |
316 | } | |
64a07bd8 SR |
317 | if (!de) { |
318 | rtn = -ENOENT; | |
319 | goto out; | |
320 | } | |
1da177e4 LT |
321 | cp += len + 1; |
322 | } | |
323 | *residual = cp; | |
324 | *ret = de; | |
64a07bd8 SR |
325 | out: |
326 | spin_unlock(&proc_subdir_lock); | |
327 | return rtn; | |
1da177e4 LT |
328 | } |
329 | ||
9a185409 | 330 | static DEFINE_IDA(proc_inum_ida); |
1da177e4 LT |
331 | static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */ |
332 | ||
67935df4 | 333 | #define PROC_DYNAMIC_FIRST 0xF0000000U |
1da177e4 LT |
334 | |
335 | /* | |
336 | * Return an inode number between PROC_DYNAMIC_FIRST and | |
337 | * 0xffffffff, or zero on failure. | |
1681bc30 RD |
338 | * |
339 | * Current inode allocations in the proc-fs (hex-numbers): | |
340 | * | |
341 | * 00000000 reserved | |
342 | * 00000001-00000fff static entries (goners) | |
343 | * 001 root-ino | |
344 | * | |
345 | * 00001000-00001fff unused | |
346 | * 0001xxxx-7fffxxxx pid-dir entries for pid 1-7fff | |
347 | * 80000000-efffffff unused | |
348 | * f0000000-ffffffff dynamic entries | |
349 | * | |
350 | * Goal: | |
351 | * Once we split the thing into several virtual filesystems, | |
352 | * we will get rid of magical ranges (and this comment, BTW). | |
1da177e4 LT |
353 | */ |
354 | static unsigned int get_inode_number(void) | |
355 | { | |
67935df4 | 356 | unsigned int i; |
1da177e4 LT |
357 | int error; |
358 | ||
359 | retry: | |
9a185409 | 360 | if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0) |
1da177e4 LT |
361 | return 0; |
362 | ||
363 | spin_lock(&proc_inum_lock); | |
9a185409 | 364 | error = ida_get_new(&proc_inum_ida, &i); |
1da177e4 LT |
365 | spin_unlock(&proc_inum_lock); |
366 | if (error == -EAGAIN) | |
367 | goto retry; | |
368 | else if (error) | |
369 | return 0; | |
370 | ||
67935df4 AD |
371 | if (i > UINT_MAX - PROC_DYNAMIC_FIRST) { |
372 | spin_lock(&proc_inum_lock); | |
9a185409 | 373 | ida_remove(&proc_inum_ida, i); |
67935df4 | 374 | spin_unlock(&proc_inum_lock); |
cc996099 | 375 | return 0; |
67935df4 AD |
376 | } |
377 | return PROC_DYNAMIC_FIRST + i; | |
1da177e4 LT |
378 | } |
379 | ||
380 | static void release_inode_number(unsigned int inum) | |
381 | { | |
1da177e4 | 382 | spin_lock(&proc_inum_lock); |
9a185409 | 383 | ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); |
1da177e4 LT |
384 | spin_unlock(&proc_inum_lock); |
385 | } | |
386 | ||
008b150a | 387 | static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
388 | { |
389 | nd_set_link(nd, PDE(dentry->d_inode)->data); | |
008b150a | 390 | return NULL; |
1da177e4 LT |
391 | } |
392 | ||
c5ef1c42 | 393 | static const struct inode_operations proc_link_inode_operations = { |
1da177e4 LT |
394 | .readlink = generic_readlink, |
395 | .follow_link = proc_follow_link, | |
396 | }; | |
397 | ||
398 | /* | |
399 | * As some entries in /proc are volatile, we want to | |
400 | * get rid of unused dentries. This could be made | |
401 | * smarter: we could keep a "volatile" flag in the | |
402 | * inode to indicate which ones to keep. | |
403 | */ | |
404 | static int proc_delete_dentry(struct dentry * dentry) | |
405 | { | |
406 | return 1; | |
407 | } | |
408 | ||
d72f71eb | 409 | static const struct dentry_operations proc_dentry_operations = |
1da177e4 LT |
410 | { |
411 | .d_delete = proc_delete_dentry, | |
412 | }; | |
413 | ||
414 | /* | |
415 | * Don't create negative dentries here, return -ENOENT by hand | |
416 | * instead. | |
417 | */ | |
e9720acd PE |
418 | struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, |
419 | struct dentry *dentry) | |
1da177e4 LT |
420 | { |
421 | struct inode *inode = NULL; | |
1da177e4 LT |
422 | int error = -ENOENT; |
423 | ||
64a07bd8 | 424 | spin_lock(&proc_subdir_lock); |
5e971dce AD |
425 | for (de = de->subdir; de ; de = de->next) { |
426 | if (de->namelen != dentry->d_name.len) | |
427 | continue; | |
428 | if (!memcmp(dentry->d_name.name, de->name, de->namelen)) { | |
429 | unsigned int ino; | |
430 | ||
431 | ino = de->low_ino; | |
432 | de_get(de); | |
433 | spin_unlock(&proc_subdir_lock); | |
434 | error = -EINVAL; | |
435 | inode = proc_get_inode(dir->i_sb, ino, de); | |
436 | goto out_unlock; | |
1da177e4 LT |
437 | } |
438 | } | |
64a07bd8 | 439 | spin_unlock(&proc_subdir_lock); |
4237e0d3 | 440 | out_unlock: |
1da177e4 LT |
441 | |
442 | if (inode) { | |
443 | dentry->d_op = &proc_dentry_operations; | |
444 | d_add(dentry, inode); | |
445 | return NULL; | |
446 | } | |
5e971dce AD |
447 | if (de) |
448 | de_put(de); | |
1da177e4 LT |
449 | return ERR_PTR(error); |
450 | } | |
451 | ||
e9720acd PE |
452 | struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, |
453 | struct nameidata *nd) | |
454 | { | |
455 | return proc_lookup_de(PDE(dir), dir, dentry); | |
456 | } | |
457 | ||
1da177e4 LT |
458 | /* |
459 | * This returns non-zero if at EOF, so that the /proc | |
460 | * root directory can use this and check if it should | |
461 | * continue with the <pid> entries.. | |
462 | * | |
463 | * Note that the VFS-layer doesn't care about the return | |
464 | * value of the readdir() call, as long as it's non-negative | |
465 | * for success.. | |
466 | */ | |
e9720acd PE |
467 | int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, |
468 | filldir_t filldir) | |
1da177e4 | 469 | { |
1da177e4 LT |
470 | unsigned int ino; |
471 | int i; | |
2fddfeef | 472 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
473 | int ret = 0; |
474 | ||
1da177e4 | 475 | ino = inode->i_ino; |
1da177e4 LT |
476 | i = filp->f_pos; |
477 | switch (i) { | |
478 | case 0: | |
479 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) | |
480 | goto out; | |
481 | i++; | |
482 | filp->f_pos++; | |
483 | /* fall through */ | |
484 | case 1: | |
485 | if (filldir(dirent, "..", 2, i, | |
2fddfeef | 486 | parent_ino(filp->f_path.dentry), |
1da177e4 LT |
487 | DT_DIR) < 0) |
488 | goto out; | |
489 | i++; | |
490 | filp->f_pos++; | |
491 | /* fall through */ | |
492 | default: | |
64a07bd8 | 493 | spin_lock(&proc_subdir_lock); |
1da177e4 LT |
494 | de = de->subdir; |
495 | i -= 2; | |
496 | for (;;) { | |
497 | if (!de) { | |
498 | ret = 1; | |
64a07bd8 | 499 | spin_unlock(&proc_subdir_lock); |
1da177e4 LT |
500 | goto out; |
501 | } | |
502 | if (!i) | |
503 | break; | |
504 | de = de->next; | |
505 | i--; | |
506 | } | |
507 | ||
508 | do { | |
59cd0cbc DW |
509 | struct proc_dir_entry *next; |
510 | ||
64a07bd8 | 511 | /* filldir passes info to user space */ |
59cd0cbc | 512 | de_get(de); |
64a07bd8 | 513 | spin_unlock(&proc_subdir_lock); |
1da177e4 | 514 | if (filldir(dirent, de->name, de->namelen, filp->f_pos, |
59cd0cbc DW |
515 | de->low_ino, de->mode >> 12) < 0) { |
516 | de_put(de); | |
1da177e4 | 517 | goto out; |
59cd0cbc | 518 | } |
64a07bd8 | 519 | spin_lock(&proc_subdir_lock); |
1da177e4 | 520 | filp->f_pos++; |
59cd0cbc DW |
521 | next = de->next; |
522 | de_put(de); | |
523 | de = next; | |
1da177e4 | 524 | } while (de); |
64a07bd8 | 525 | spin_unlock(&proc_subdir_lock); |
1da177e4 LT |
526 | } |
527 | ret = 1; | |
b4df2b92 | 528 | out: |
1da177e4 LT |
529 | return ret; |
530 | } | |
531 | ||
e9720acd PE |
532 | int proc_readdir(struct file *filp, void *dirent, filldir_t filldir) |
533 | { | |
534 | struct inode *inode = filp->f_path.dentry->d_inode; | |
535 | ||
536 | return proc_readdir_de(PDE(inode), filp, dirent, filldir); | |
537 | } | |
538 | ||
1da177e4 LT |
539 | /* |
540 | * These are the generic /proc directory operations. They | |
541 | * use the in-memory "struct proc_dir_entry" tree to parse | |
542 | * the /proc directory. | |
543 | */ | |
00977a59 | 544 | static const struct file_operations proc_dir_operations = { |
b4df2b92 | 545 | .llseek = generic_file_llseek, |
1da177e4 LT |
546 | .read = generic_read_dir, |
547 | .readdir = proc_readdir, | |
548 | }; | |
549 | ||
550 | /* | |
551 | * proc directories can do almost nothing.. | |
552 | */ | |
c5ef1c42 | 553 | static const struct inode_operations proc_dir_inode_operations = { |
1da177e4 | 554 | .lookup = proc_lookup, |
2b579bee | 555 | .getattr = proc_getattr, |
1da177e4 LT |
556 | .setattr = proc_notify_change, |
557 | }; | |
558 | ||
559 | static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp) | |
560 | { | |
561 | unsigned int i; | |
94413d88 | 562 | struct proc_dir_entry *tmp; |
1da177e4 LT |
563 | |
564 | i = get_inode_number(); | |
565 | if (i == 0) | |
566 | return -EAGAIN; | |
567 | dp->low_ino = i; | |
64a07bd8 | 568 | |
1da177e4 LT |
569 | if (S_ISDIR(dp->mode)) { |
570 | if (dp->proc_iops == NULL) { | |
571 | dp->proc_fops = &proc_dir_operations; | |
572 | dp->proc_iops = &proc_dir_inode_operations; | |
573 | } | |
574 | dir->nlink++; | |
575 | } else if (S_ISLNK(dp->mode)) { | |
576 | if (dp->proc_iops == NULL) | |
577 | dp->proc_iops = &proc_link_inode_operations; | |
578 | } else if (S_ISREG(dp->mode)) { | |
579 | if (dp->proc_fops == NULL) | |
580 | dp->proc_fops = &proc_file_operations; | |
581 | if (dp->proc_iops == NULL) | |
582 | dp->proc_iops = &proc_file_inode_operations; | |
583 | } | |
99fc06df CG |
584 | |
585 | spin_lock(&proc_subdir_lock); | |
94413d88 ZR |
586 | |
587 | for (tmp = dir->subdir; tmp; tmp = tmp->next) | |
588 | if (strcmp(tmp->name, dp->name) == 0) { | |
6c2f91e0 | 589 | WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n", |
665020c3 | 590 | dir->name, dp->name); |
94413d88 ZR |
591 | break; |
592 | } | |
593 | ||
99fc06df CG |
594 | dp->next = dir->subdir; |
595 | dp->parent = dir; | |
596 | dir->subdir = dp; | |
597 | spin_unlock(&proc_subdir_lock); | |
598 | ||
1da177e4 LT |
599 | return 0; |
600 | } | |
601 | ||
2d3a4e36 | 602 | static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, |
1da177e4 LT |
603 | const char *name, |
604 | mode_t mode, | |
605 | nlink_t nlink) | |
606 | { | |
607 | struct proc_dir_entry *ent = NULL; | |
608 | const char *fn = name; | |
609 | int len; | |
610 | ||
611 | /* make sure name is valid */ | |
612 | if (!name || !strlen(name)) goto out; | |
613 | ||
7cee4e00 | 614 | if (xlate_proc_name(name, parent, &fn) != 0) |
1da177e4 LT |
615 | goto out; |
616 | ||
617 | /* At this point there must not be any '/' characters beyond *fn */ | |
618 | if (strchr(fn, '/')) | |
619 | goto out; | |
620 | ||
621 | len = strlen(fn); | |
622 | ||
623 | ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL); | |
624 | if (!ent) goto out; | |
625 | ||
626 | memset(ent, 0, sizeof(struct proc_dir_entry)); | |
627 | memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1); | |
628 | ent->name = ((char *) ent) + sizeof(*ent); | |
629 | ent->namelen = len; | |
630 | ent->mode = mode; | |
631 | ent->nlink = nlink; | |
5a622f2d | 632 | atomic_set(&ent->count, 1); |
786d7e16 AD |
633 | ent->pde_users = 0; |
634 | spin_lock_init(&ent->pde_unload_lock); | |
635 | ent->pde_unload_completion = NULL; | |
881adb85 | 636 | INIT_LIST_HEAD(&ent->pde_openers); |
1da177e4 LT |
637 | out: |
638 | return ent; | |
639 | } | |
640 | ||
641 | struct proc_dir_entry *proc_symlink(const char *name, | |
642 | struct proc_dir_entry *parent, const char *dest) | |
643 | { | |
644 | struct proc_dir_entry *ent; | |
645 | ||
2d3a4e36 | 646 | ent = __proc_create(&parent, name, |
1da177e4 LT |
647 | (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); |
648 | ||
649 | if (ent) { | |
650 | ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL); | |
651 | if (ent->data) { | |
652 | strcpy((char*)ent->data,dest); | |
653 | if (proc_register(parent, ent) < 0) { | |
654 | kfree(ent->data); | |
655 | kfree(ent); | |
656 | ent = NULL; | |
657 | } | |
658 | } else { | |
659 | kfree(ent); | |
660 | ent = NULL; | |
661 | } | |
662 | } | |
663 | return ent; | |
664 | } | |
665 | ||
666 | struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode, | |
667 | struct proc_dir_entry *parent) | |
668 | { | |
669 | struct proc_dir_entry *ent; | |
670 | ||
2d3a4e36 | 671 | ent = __proc_create(&parent, name, S_IFDIR | mode, 2); |
1da177e4 | 672 | if (ent) { |
1da177e4 LT |
673 | if (proc_register(parent, ent) < 0) { |
674 | kfree(ent); | |
675 | ent = NULL; | |
676 | } | |
677 | } | |
678 | return ent; | |
679 | } | |
680 | ||
78e92b99 DL |
681 | struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name, |
682 | struct proc_dir_entry *parent) | |
683 | { | |
684 | struct proc_dir_entry *ent; | |
685 | ||
686 | ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2); | |
687 | if (ent) { | |
688 | ent->data = net; | |
689 | if (proc_register(parent, ent) < 0) { | |
690 | kfree(ent); | |
691 | ent = NULL; | |
692 | } | |
693 | } | |
694 | return ent; | |
695 | } | |
696 | EXPORT_SYMBOL_GPL(proc_net_mkdir); | |
697 | ||
1da177e4 LT |
698 | struct proc_dir_entry *proc_mkdir(const char *name, |
699 | struct proc_dir_entry *parent) | |
700 | { | |
701 | return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent); | |
702 | } | |
703 | ||
704 | struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, | |
705 | struct proc_dir_entry *parent) | |
706 | { | |
707 | struct proc_dir_entry *ent; | |
708 | nlink_t nlink; | |
709 | ||
710 | if (S_ISDIR(mode)) { | |
711 | if ((mode & S_IALLUGO) == 0) | |
712 | mode |= S_IRUGO | S_IXUGO; | |
713 | nlink = 2; | |
714 | } else { | |
715 | if ((mode & S_IFMT) == 0) | |
716 | mode |= S_IFREG; | |
717 | if ((mode & S_IALLUGO) == 0) | |
718 | mode |= S_IRUGO; | |
719 | nlink = 1; | |
720 | } | |
721 | ||
2d3a4e36 | 722 | ent = __proc_create(&parent, name, mode, nlink); |
1da177e4 | 723 | if (ent) { |
1da177e4 LT |
724 | if (proc_register(parent, ent) < 0) { |
725 | kfree(ent); | |
726 | ent = NULL; | |
727 | } | |
728 | } | |
729 | return ent; | |
730 | } | |
731 | ||
59b74351 DL |
732 | struct proc_dir_entry *proc_create_data(const char *name, mode_t mode, |
733 | struct proc_dir_entry *parent, | |
734 | const struct file_operations *proc_fops, | |
735 | void *data) | |
2d3a4e36 AD |
736 | { |
737 | struct proc_dir_entry *pde; | |
738 | nlink_t nlink; | |
739 | ||
740 | if (S_ISDIR(mode)) { | |
741 | if ((mode & S_IALLUGO) == 0) | |
742 | mode |= S_IRUGO | S_IXUGO; | |
743 | nlink = 2; | |
744 | } else { | |
745 | if ((mode & S_IFMT) == 0) | |
746 | mode |= S_IFREG; | |
747 | if ((mode & S_IALLUGO) == 0) | |
748 | mode |= S_IRUGO; | |
749 | nlink = 1; | |
750 | } | |
751 | ||
752 | pde = __proc_create(&parent, name, mode, nlink); | |
753 | if (!pde) | |
754 | goto out; | |
755 | pde->proc_fops = proc_fops; | |
59b74351 | 756 | pde->data = data; |
2d3a4e36 AD |
757 | if (proc_register(parent, pde) < 0) |
758 | goto out_free; | |
759 | return pde; | |
760 | out_free: | |
761 | kfree(pde); | |
762 | out: | |
763 | return NULL; | |
764 | } | |
765 | ||
1da177e4 LT |
766 | void free_proc_entry(struct proc_dir_entry *de) |
767 | { | |
768 | unsigned int ino = de->low_ino; | |
769 | ||
770 | if (ino < PROC_DYNAMIC_FIRST) | |
771 | return; | |
772 | ||
773 | release_inode_number(ino); | |
774 | ||
fd2cbe48 | 775 | if (S_ISLNK(de->mode)) |
1da177e4 LT |
776 | kfree(de->data); |
777 | kfree(de); | |
778 | } | |
779 | ||
780 | /* | |
781 | * Remove a /proc entry and free it if it's not currently in use. | |
1da177e4 LT |
782 | */ |
783 | void remove_proc_entry(const char *name, struct proc_dir_entry *parent) | |
784 | { | |
785 | struct proc_dir_entry **p; | |
f649d6d3 | 786 | struct proc_dir_entry *de = NULL; |
1da177e4 LT |
787 | const char *fn = name; |
788 | int len; | |
789 | ||
7cee4e00 | 790 | if (xlate_proc_name(name, &parent, &fn) != 0) |
f649d6d3 | 791 | return; |
1da177e4 | 792 | len = strlen(fn); |
64a07bd8 SR |
793 | |
794 | spin_lock(&proc_subdir_lock); | |
1da177e4 | 795 | for (p = &parent->subdir; *p; p=&(*p)->next ) { |
f649d6d3 AD |
796 | if (proc_match(len, fn, *p)) { |
797 | de = *p; | |
798 | *p = de->next; | |
799 | de->next = NULL; | |
800 | break; | |
801 | } | |
802 | } | |
803 | spin_unlock(&proc_subdir_lock); | |
804 | if (!de) | |
805 | return; | |
786d7e16 | 806 | |
f649d6d3 AD |
807 | spin_lock(&de->pde_unload_lock); |
808 | /* | |
809 | * Stop accepting new callers into module. If you're | |
810 | * dynamically allocating ->proc_fops, save a pointer somewhere. | |
811 | */ | |
812 | de->proc_fops = NULL; | |
813 | /* Wait until all existing callers into module are done. */ | |
814 | if (de->pde_users > 0) { | |
815 | DECLARE_COMPLETION_ONSTACK(c); | |
816 | ||
817 | if (!de->pde_unload_completion) | |
818 | de->pde_unload_completion = &c; | |
786d7e16 | 819 | |
786d7e16 AD |
820 | spin_unlock(&de->pde_unload_lock); |
821 | ||
f649d6d3 AD |
822 | wait_for_completion(de->pde_unload_completion); |
823 | ||
824 | goto continue_removing; | |
825 | } | |
826 | spin_unlock(&de->pde_unload_lock); | |
827 | ||
786d7e16 | 828 | continue_removing: |
881adb85 AD |
829 | spin_lock(&de->pde_unload_lock); |
830 | while (!list_empty(&de->pde_openers)) { | |
831 | struct pde_opener *pdeo; | |
832 | ||
833 | pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh); | |
834 | list_del(&pdeo->lh); | |
835 | spin_unlock(&de->pde_unload_lock); | |
836 | pdeo->release(pdeo->inode, pdeo->file); | |
837 | kfree(pdeo); | |
838 | spin_lock(&de->pde_unload_lock); | |
839 | } | |
840 | spin_unlock(&de->pde_unload_lock); | |
841 | ||
f649d6d3 AD |
842 | if (S_ISDIR(de->mode)) |
843 | parent->nlink--; | |
844 | de->nlink = 0; | |
267e2a9c | 845 | WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory " |
f649d6d3 AD |
846 | "'%s/%s', leaking at least '%s'\n", __func__, |
847 | de->parent->name, de->name, de->subdir->name); | |
f649d6d3 AD |
848 | if (atomic_dec_and_test(&de->count)) |
849 | free_proc_entry(de); | |
1da177e4 | 850 | } |