kernel debug: priv can be NULL early on
[minix.git] / lib / libsffs / verify.c
blob1d4eab9c995ba4cdd29f1e5b44a04815d83f8588
1 /* This file contains routines that verify inodes and paths against the host.
3 * The entry points into this file are:
4 * verify_path check whether a path,inode pair is still valid
5 * verify_inode construct a path for an inode and verify the inode
6 * verify_dentry check a directory inode and look for a directory entry
8 * Created:
9 * April 2009 (D.C. van Moolenbroek)
12 #include "inc.h"
14 /*===========================================================================*
15 * verify_path *
16 *===========================================================================*/
17 int verify_path(path, ino, attr, stale)
18 char path[PATH_MAX];
19 struct inode *ino;
20 struct sffs_attr *attr;
21 int *stale;
23 /* Given a path, and the inode associated with that path, verify if the inode
24 * still matches the real world. Obtain the attributes of the file identified
25 * by the given path, and see if they match. If not, possibly mark the inode
26 * as deleted and return an error. Only upon success is the inode guaranteed
27 * to be usable.
29 * The caller must set the a_mask field of the passed attr struct.
30 * If 'stale' is not NULL, the value it points to must be initialized to 0,
31 * and will be set to 1 if the path was valid but the inode wasn't.
33 int r;
35 attr->a_mask |= SFFS_ATTR_MODE;
37 r = sffs_table->t_getattr(path, attr);
39 dprintf(("%s: verify_path: getattr('%s') returned %d\n",
40 sffs_name, path, r));
42 if (r != OK) {
43 /* If we are told that the path does not exist, delete the inode */
44 if (r == ENOENT || r == ENOTDIR)
45 del_dentry(ino);
47 return r; /* path isn't valid */
50 /* If the file type (reg, dir) isn't what we thought, delete the inode */
51 if ((ino->i_flags & I_DIR) != MODE_TO_DIRFLAG(attr->a_mode)) {
52 del_dentry(ino);
54 if (stale != NULL) *stale = 1;
55 return ENOENT; /* path is valid, inode wasn't */
58 return OK; /* path and inode are valid */
61 /*===========================================================================*
62 * verify_inode *
63 *===========================================================================*/
64 int verify_inode(ino, path, attr)
65 struct inode *ino; /* inode to verify */
66 char path[PATH_MAX]; /* buffer in which to store the path */
67 struct sffs_attr *attr; /* buffer for attributes, or NULL */
69 /* Given an inode, construct a path identifying the inode, and check whether
70 * that path is still valid for that inode (as far as we can tell). As a side
71 * effect, store attributes in the given attribute structure if not NULL (its
72 * a_mask member must then be set).
74 struct sffs_attr attr2;
75 int r;
77 if ((r = make_path(path, ino)) != OK) return r;
79 if (attr == NULL) {
80 attr2.a_mask = 0;
82 attr = &attr2;
85 return verify_path(path, ino, attr, NULL);
88 /*===========================================================================*
89 * verify_dentry *
90 *===========================================================================*/
91 int verify_dentry(parent, name, path, res_ino)
92 struct inode *parent; /* parent inode: the inode to verify */
93 char name[NAME_MAX+1]; /* the given directory entry path component */
94 char path[PATH_MAX]; /* buffer to store the resulting path in */
95 struct inode **res_ino; /* pointer for addressed inode (or NULL) */
97 /* Given a directory inode and a name, construct a path identifying that
98 * directory entry, check whether the path to the parent is still valid, and
99 * check whether there is an inode pointed to by the full path. Upon success,
100 * res_ino will contain either the inode for the full path, with increased
101 * refcount, or NULL if no such inode exists.
103 int r;
105 if ((r = verify_inode(parent, path, NULL)) != OK)
106 return r;
108 dprintf(("%s: verify_dentry: given path is '%s', name '%s'\n",
109 sffs_name, path, name));
111 if ((r = push_path(path, name)) != OK)
112 return r;
114 dprintf(("%s: verify_dentry: path now '%s'\n", sffs_name, path));
116 *res_ino = lookup_dentry(parent, name);
118 return OK;