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