781
791
error = __vfs_follow_link(nd, s);
782
792
else if (nd->last_type == LAST_BIND) {
783
error = force_reval_path(&nd->path, nd);
793
nd->flags |= LOOKUP_JUMPED;
794
nd->inode = nd->path.dentry->d_inode;
795
if (nd->inode->i_op->follow_link) {
796
/* stepped on a _really_ weird one */
785
797
path_put(&nd->path);
792
* This limits recursive symlink follows to 8, while
793
* limiting consecutive symlinks to 40.
795
* Without that kind of total limit, nasty chains of consecutive
796
* symlinks can cause almost arbitrarily long lookups.
798
static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
803
/* We drop rcu-walk here */
804
if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
806
BUG_ON(inode != path->dentry->d_inode);
808
if (current->link_count >= MAX_NESTED_LINKS)
810
if (current->total_link_count >= 40)
812
BUG_ON(nd->depth >= MAX_NESTED_LINKS);
814
err = security_inode_follow_link(path->dentry, nd);
817
current->link_count++;
818
current->total_link_count++;
820
err = __do_follow_link(path, nd, &cookie);
821
if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
822
path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
824
current->link_count--;
828
path_put_conditional(path, nd);
833
805
static int follow_up_rcu(struct path *path)
835
807
struct vfsmount *parent;
1217
1208
struct vfsmount *mnt = nd->path.mnt;
1218
1209
struct dentry *dentry, *parent = nd->path.dentry;
1223
* See if the low-level filesystem might want
1224
* to use its own hash..
1226
if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1227
err = parent->d_op->d_hash(parent, nd->inode, name);
1233
1215
* Rename seqlock is not required here because in the off chance
1234
1216
* of a false negative due to a concurrent rename, we're going to
1235
1217
* do the non-racy lookup, below.
1237
1219
if (nd->flags & LOOKUP_RCU) {
1240
1221
*inode = nd->inode;
1241
1222
dentry = __d_lookup_rcu(parent, name, &seq, inode);
1243
if (nameidata_drop_rcu(nd))
1247
1226
/* Memory barrier in read_seqcount_begin of child is enough */
1248
1227
if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1249
1228
return -ECHILD;
1252
1231
if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1253
dentry = do_revalidate_rcu(dentry, nd);
1258
if (!(nd->flags & LOOKUP_RCU))
1232
status = d_revalidate(dentry, nd);
1233
if (unlikely(status <= 0)) {
1234
if (status != -ECHILD)
1261
1239
path->mnt = mnt;
1262
1240
path->dentry = dentry;
1263
1241
if (likely(__follow_mount_rcu(nd, path, inode, false)))
1265
if (nameidata_drop_rcu(nd))
1269
dentry = __d_lookup(parent, name);
1273
if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1274
dentry = do_revalidate(dentry, nd);
1245
if (nameidata_dentry_drop_rcu(nd, dentry))
1248
if (nameidata_drop_rcu(nd))
1252
dentry = __d_lookup(parent, name);
1256
if (unlikely(!dentry)) {
1257
struct inode *dir = parent->d_inode;
1258
BUG_ON(nd->inode != dir);
1260
mutex_lock(&dir->i_mutex);
1261
dentry = d_lookup(parent, name);
1262
if (likely(!dentry)) {
1263
dentry = d_alloc_and_lookup(parent, name, nd);
1264
if (IS_ERR(dentry)) {
1265
mutex_unlock(&dir->i_mutex);
1266
return PTR_ERR(dentry);
1272
mutex_unlock(&dir->i_mutex);
1274
if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1275
status = d_revalidate(dentry, nd);
1276
if (unlikely(status <= 0)) {
1281
if (!d_invalidate(dentry)) {
1281
1289
path->mnt = mnt;
1282
1290
path->dentry = dentry;
1283
1291
err = follow_managed(path, nd->flags);
1288
1296
*inode = path->dentry->d_inode;
1292
dir = parent->d_inode;
1293
BUG_ON(nd->inode != dir);
1295
mutex_lock(&dir->i_mutex);
1297
* First re-do the cached lookup just in case it was created
1298
* while we waited for the directory semaphore, or the first
1299
* lookup failed due to an unrelated rename.
1301
* This could use version numbering or similar to avoid unnecessary
1302
* cache lookups, but then we'd have to do the first lookup in the
1303
* non-racy way. However in the common case here, everything should
1304
* be hot in cache, so would it be a big win?
1306
dentry = d_lookup(parent, name);
1307
if (likely(!dentry)) {
1308
dentry = d_alloc_and_lookup(parent, name, nd);
1309
mutex_unlock(&dir->i_mutex);
1315
* Uhhuh! Nasty case: the cache was re-populated while
1316
* we waited on the semaphore. Need to revalidate.
1318
mutex_unlock(&dir->i_mutex);
1322
return PTR_ERR(dentry);
1300
static inline int may_lookup(struct nameidata *nd)
1302
if (nd->flags & LOOKUP_RCU) {
1303
int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1306
if (nameidata_drop_rcu(nd))
1309
return exec_permission(nd->inode, 0);
1312
static inline int handle_dots(struct nameidata *nd, int type)
1314
if (type == LAST_DOTDOT) {
1315
if (nd->flags & LOOKUP_RCU) {
1316
if (follow_dotdot_rcu(nd))
1324
static void terminate_walk(struct nameidata *nd)
1326
if (!(nd->flags & LOOKUP_RCU)) {
1327
path_put(&nd->path);
1329
nd->flags &= ~LOOKUP_RCU;
1330
if (!(nd->flags & LOOKUP_ROOT))
1331
nd->root.mnt = NULL;
1333
br_read_unlock(vfsmount_lock);
1337
static inline int walk_component(struct nameidata *nd, struct path *path,
1338
struct qstr *name, int type, int follow)
1340
struct inode *inode;
1343
* "." and ".." are special - ".." especially so because it has
1344
* to be able to know about the current root directory and
1345
* parent relationships.
1347
if (unlikely(type != LAST_NORM))
1348
return handle_dots(nd, type);
1349
err = do_lookup(nd, name, path, &inode);
1350
if (unlikely(err)) {
1355
path_to_nameidata(path, nd);
1359
if (unlikely(inode->i_op->follow_link) && follow) {
1360
if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
1362
BUG_ON(inode != path->dentry->d_inode);
1365
path_to_nameidata(path, nd);
1371
* This limits recursive symlink follows to 8, while
1372
* limiting consecutive symlinks to 40.
1374
* Without that kind of total limit, nasty chains of consecutive
1375
* symlinks can cause almost arbitrarily long lookups.
1377
static inline int nested_symlink(struct path *path, struct nameidata *nd)
1381
BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1382
if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1383
path_put_conditional(path, nd);
1384
path_put(&nd->path);
1389
current->link_count++;
1392
struct path link = *path;
1395
res = follow_link(&link, nd, &cookie);
1397
res = walk_component(nd, path, &nd->last,
1398
nd->last_type, LOOKUP_FOLLOW);
1399
put_link(nd, &link, cookie);
1402
current->link_count--;
1378
1448
this.len = name - (const char *) this.name;
1379
1449
this.hash = end_name_hash(hash);
1452
if (this.name[0] == '.') switch (this.len) {
1454
if (this.name[1] == '.') {
1456
nd->flags |= LOOKUP_JUMPED;
1462
if (likely(type == LAST_NORM)) {
1463
struct dentry *parent = nd->path.dentry;
1464
nd->flags &= ~LOOKUP_JUMPED;
1465
if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1466
err = parent->d_op->d_hash(parent, nd->inode,
1381
1473
/* remove trailing slashes? */
1383
1475
goto last_component;
1384
1476
while (*++name == '/');
1386
goto last_with_slashes;
1389
* "." and ".." are special - ".." especially so because it has
1390
* to be able to know about the current root directory and
1391
* parent relationships.
1393
if (this.name[0] == '.') switch (this.len) {
1397
if (this.name[1] != '.')
1399
if (nd->flags & LOOKUP_RCU) {
1400
if (follow_dotdot_rcu(nd))
1408
/* This does the actual lookups.. */
1409
err = do_lookup(nd, &this, &next, &inode);
1416
if (inode->i_op->follow_link) {
1417
err = do_follow_link(inode, &next, nd);
1478
goto last_component;
1480
err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1485
err = nested_symlink(&next, nd);
1420
nd->inode = nd->path.dentry->d_inode;
1425
path_to_nameidata(&next, nd);
1428
1489
err = -ENOTDIR;
1429
1490
if (!nd->inode->i_op->lookup)
1432
1493
/* here ends the main loop */
1435
lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1436
1495
last_component:
1437
1496
/* Clear LOOKUP_CONTINUE iff it was previously unset */
1438
1497
nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1439
if (lookup_flags & LOOKUP_PARENT)
1441
if (this.name[0] == '.') switch (this.len) {
1445
if (this.name[1] != '.')
1447
if (nd->flags & LOOKUP_RCU) {
1448
if (follow_dotdot_rcu(nd))
1456
err = do_lookup(nd, &this, &next, &inode);
1459
if (inode && unlikely(inode->i_op->follow_link) &&
1460
(lookup_flags & LOOKUP_FOLLOW)) {
1461
err = do_follow_link(inode, &next, nd);
1464
nd->inode = nd->path.dentry->d_inode;
1466
path_to_nameidata(&next, nd);
1472
if (lookup_flags & LOOKUP_DIRECTORY) {
1474
if (!nd->inode->i_op->lookup)
1479
1498
nd->last = this;
1480
nd->last_type = LAST_NORM;
1481
if (this.name[0] != '.')
1484
nd->last_type = LAST_DOT;
1485
else if (this.len == 2 && this.name[1] == '.')
1486
nd->last_type = LAST_DOTDOT;
1491
* We bypassed the ordinary revalidation routines.
1492
* We may need to check the cached dentry for staleness.
1494
if (need_reval_dot(nd->path.dentry)) {
1495
if (nameidata_drop_rcu_last_maybe(nd))
1497
/* Note: we do not d_invalidate() */
1498
err = d_revalidate(nd->path.dentry, nd);
1506
if (nameidata_drop_rcu_last_maybe(nd))
1499
nd->last_type = type;
1510
if (!(nd->flags & LOOKUP_RCU))
1511
path_put_conditional(&next, nd);
1514
if (!(nd->flags & LOOKUP_RCU))
1515
path_put(&nd->path);
1520
static inline int path_walk_rcu(const char *name, struct nameidata *nd)
1522
current->total_link_count = 0;
1524
return link_path_walk(name, nd);
1527
static inline int path_walk_simple(const char *name, struct nameidata *nd)
1529
current->total_link_count = 0;
1531
return link_path_walk(name, nd);
1534
static int path_walk(const char *name, struct nameidata *nd)
1536
struct path save = nd->path;
1539
current->total_link_count = 0;
1541
/* make sure the stuff we saved doesn't go away */
1544
result = link_path_walk(name, nd);
1545
if (result == -ESTALE) {
1546
/* nd->path had been dropped */
1547
current->total_link_count = 0;
1549
nd->inode = save.dentry->d_inode;
1550
path_get(&nd->path);
1551
nd->flags |= LOOKUP_REVAL;
1552
result = link_path_walk(name, nd);
1560
static void path_finish_rcu(struct nameidata *nd)
1562
if (nd->flags & LOOKUP_RCU) {
1563
/* RCU dangling. Cancel it. */
1564
nd->flags &= ~LOOKUP_RCU;
1565
nd->root.mnt = NULL;
1567
br_read_unlock(vfsmount_lock);
1573
static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1579
nd->last_type = LAST_ROOT; /* if there are only slashes... */
1580
nd->flags = flags | LOOKUP_RCU;
1582
nd->root.mnt = NULL;
1586
struct fs_struct *fs = current->fs;
1589
br_read_lock(vfsmount_lock);
1593
seq = read_seqcount_begin(&fs->seq);
1594
nd->root = fs->root;
1595
nd->path = nd->root;
1596
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1597
} while (read_seqcount_retry(&fs->seq, seq));
1599
} else if (dfd == AT_FDCWD) {
1600
struct fs_struct *fs = current->fs;
1603
br_read_lock(vfsmount_lock);
1607
seq = read_seqcount_begin(&fs->seq);
1609
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1610
} while (read_seqcount_retry(&fs->seq, seq));
1613
struct dentry *dentry;
1615
file = fget_light(dfd, &fput_needed);
1620
dentry = file->f_path.dentry;
1623
if (!S_ISDIR(dentry->d_inode->i_mode))
1626
retval = file_permission(file, MAY_EXEC);
1630
nd->path = file->f_path;
1634
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1635
br_read_lock(vfsmount_lock);
1638
nd->inode = nd->path.dentry->d_inode;
1642
fput_light(file, fput_needed);
1647
static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1653
nd->last_type = LAST_ROOT; /* if there are only slashes... */
1656
nd->root.mnt = NULL;
1660
nd->path = nd->root;
1661
path_get(&nd->root);
1662
} else if (dfd == AT_FDCWD) {
1663
get_fs_pwd(current->fs, &nd->path);
1665
struct dentry *dentry;
1667
file = fget_light(dfd, &fput_needed);
1672
dentry = file->f_path.dentry;
1675
if (!S_ISDIR(dentry->d_inode->i_mode))
1678
retval = file_permission(file, MAY_EXEC);
1682
nd->path = file->f_path;
1683
path_get(&file->f_path);
1685
fput_light(file, fput_needed);
1687
nd->inode = nd->path.dentry->d_inode;
1691
fput_light(file, fput_needed);
1506
static int path_init(int dfd, const char *name, unsigned int flags,
1507
struct nameidata *nd, struct file **fp)
1513
nd->last_type = LAST_ROOT; /* if there are only slashes... */
1514
nd->flags = flags | LOOKUP_JUMPED;
1516
if (flags & LOOKUP_ROOT) {
1517
struct inode *inode = nd->root.dentry->d_inode;
1519
if (!inode->i_op->lookup)
1521
retval = inode_permission(inode, MAY_EXEC);
1525
nd->path = nd->root;
1527
if (flags & LOOKUP_RCU) {
1528
br_read_lock(vfsmount_lock);
1530
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1532
path_get(&nd->path);
1537
nd->root.mnt = NULL;
1540
if (flags & LOOKUP_RCU) {
1541
br_read_lock(vfsmount_lock);
1546
path_get(&nd->root);
1548
nd->path = nd->root;
1549
} else if (dfd == AT_FDCWD) {
1550
if (flags & LOOKUP_RCU) {
1551
struct fs_struct *fs = current->fs;
1554
br_read_lock(vfsmount_lock);
1558
seq = read_seqcount_begin(&fs->seq);
1560
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1561
} while (read_seqcount_retry(&fs->seq, seq));
1563
get_fs_pwd(current->fs, &nd->path);
1566
struct dentry *dentry;
1568
file = fget_raw_light(dfd, &fput_needed);
1573
dentry = file->f_path.dentry;
1577
if (!S_ISDIR(dentry->d_inode->i_mode))
1580
retval = file_permission(file, MAY_EXEC);
1585
nd->path = file->f_path;
1586
if (flags & LOOKUP_RCU) {
1589
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1590
br_read_lock(vfsmount_lock);
1593
path_get(&file->f_path);
1594
fput_light(file, fput_needed);
1598
nd->inode = nd->path.dentry->d_inode;
1602
fput_light(file, fput_needed);
1607
static inline int lookup_last(struct nameidata *nd, struct path *path)
1609
if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1610
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1612
nd->flags &= ~LOOKUP_PARENT;
1613
return walk_component(nd, path, &nd->last, nd->last_type,
1614
nd->flags & LOOKUP_FOLLOW);
1696
1617
/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1697
static int do_path_lookup(int dfd, const char *name,
1618
static int path_lookupat(int dfd, const char *name,
1698
1619
unsigned int flags, struct nameidata *nd)
1621
struct file *base = NULL;
1703
1626
* Path walking is largely split up into 2 different synchronisation
1713
1636
* be handled by restarting a traditional ref-walk (which will always
1714
1637
* be able to complete).
1716
retval = path_init_rcu(dfd, name, flags, nd);
1717
if (unlikely(retval))
1719
retval = path_walk_rcu(name, nd);
1720
path_finish_rcu(nd);
1639
err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1644
current->total_link_count = 0;
1645
err = link_path_walk(name, nd);
1647
if (!err && !(flags & LOOKUP_PARENT)) {
1648
err = lookup_last(nd, &path);
1651
struct path link = path;
1652
nd->flags |= LOOKUP_PARENT;
1653
err = follow_link(&link, nd, &cookie);
1655
err = lookup_last(nd, &path);
1656
put_link(nd, &link, cookie);
1660
if (nd->flags & LOOKUP_RCU) {
1661
/* went all way through without dropping RCU */
1663
if (nameidata_drop_rcu_last(nd))
1668
err = handle_reval_path(nd);
1670
path_put(&nd->path);
1673
if (!err && nd->flags & LOOKUP_DIRECTORY) {
1674
if (!nd->inode->i_op->lookup) {
1675
path_put(&nd->path);
1683
if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1722
1684
path_put(&nd->root);
1723
1685
nd->root.mnt = NULL;
1726
if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
1727
/* slower, locked walk */
1728
if (retval == -ESTALE)
1729
flags |= LOOKUP_REVAL;
1730
retval = path_init(dfd, name, flags, nd);
1731
if (unlikely(retval))
1733
retval = path_walk(name, nd);
1735
path_put(&nd->root);
1736
nd->root.mnt = NULL;
1690
static int do_path_lookup(int dfd, const char *name,
1691
unsigned int flags, struct nameidata *nd)
1693
int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1694
if (unlikely(retval == -ECHILD))
1695
retval = path_lookupat(dfd, name, flags, nd);
1696
if (unlikely(retval == -ESTALE))
1697
retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1740
1699
if (likely(!retval)) {
1741
1700
if (unlikely(!audit_dummy_context())) {
2206
static int open_will_truncate(int flag, struct inode *inode)
2209
* We'll never write to the fs underlying
2212
if (special_file(inode->i_mode))
2214
return (flag & O_TRUNC);
2217
static struct file *finish_open(struct nameidata *nd,
2218
int open_flag, int acc_mode)
2224
will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2225
if (will_truncate) {
2226
error = mnt_want_write(nd->path.mnt);
2230
error = may_open(&nd->path, acc_mode, open_flag);
2233
mnt_drop_write(nd->path.mnt);
2236
filp = nameidata_to_filp(nd);
2237
if (!IS_ERR(filp)) {
2238
error = ima_file_check(filp, acc_mode);
2241
filp = ERR_PTR(error);
2244
if (!IS_ERR(filp)) {
2245
if (will_truncate) {
2246
error = handle_truncate(filp);
2249
filp = ERR_PTR(error);
2254
* It is now safe to drop the mnt write
2255
* because the filp has had a write taken
2259
mnt_drop_write(nd->path.mnt);
2260
path_put(&nd->path);
2264
path_put(&nd->path);
2265
return ERR_PTR(error);
2269
* Handle O_CREAT case for do_filp_open
2112
* Handle the last step of open()
2271
2114
static struct file *do_last(struct nameidata *nd, struct path *path,
2272
int open_flag, int acc_mode,
2273
int mode, const char *pathname)
2115
const struct open_flags *op, const char *pathname)
2275
2117
struct dentry *dir = nd->path.dentry;
2118
struct dentry *dentry;
2119
int open_flag = op->open_flag;
2120
int will_truncate = open_flag & O_TRUNC;
2122
int acc_mode = op->acc_mode;
2276
2123
struct file *filp;
2277
int error = -EISDIR;
2126
nd->flags &= ~LOOKUP_PARENT;
2127
nd->flags |= op->intent;
2279
2129
switch (nd->last_type) {
2280
2130
case LAST_DOTDOT:
2282
dir = nd->path.dentry;
2284
if (need_reval_dot(dir)) {
2285
int status = d_revalidate(nd->path.dentry, nd);
2132
error = handle_dots(nd, nd->last_type);
2134
return ERR_PTR(error);
2293
2135
/* fallthrough */
2294
2136
case LAST_ROOT:
2137
if (nd->flags & LOOKUP_RCU) {
2138
if (nameidata_drop_rcu_last(nd))
2139
return ERR_PTR(-ECHILD);
2141
error = handle_reval_path(nd);
2144
audit_inode(pathname, nd->path.dentry);
2145
if (open_flag & O_CREAT) {
2296
2150
case LAST_BIND:
2151
/* can't be RCU mode here */
2152
error = handle_reval_path(nd);
2297
2155
audit_inode(pathname, dir);
2159
if (!(open_flag & O_CREAT)) {
2161
if (nd->last.name[nd->last.len])
2162
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2163
if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2165
/* we _can_ be in RCU mode here */
2166
error = walk_component(nd, path, &nd->last, LAST_NORM,
2169
return ERR_PTR(error);
2170
if (error) /* symlink */
2173
if (nd->flags & LOOKUP_RCU) {
2174
if (nameidata_drop_rcu_last(nd))
2175
return ERR_PTR(-ECHILD);
2179
if (nd->flags & LOOKUP_DIRECTORY) {
2180
if (!nd->inode->i_op->lookup)
2183
audit_inode(pathname, nd->path.dentry);
2187
/* create side of things */
2189
if (nd->flags & LOOKUP_RCU) {
2190
if (nameidata_drop_rcu_last(nd))
2191
return ERR_PTR(-ECHILD);
2194
audit_inode(pathname, dir);
2301
2196
/* trailing slashes? */
2302
2197
if (nd->last.name[nd->last.len])
2305
2200
mutex_lock(&dir->d_inode->i_mutex);
2307
path->dentry = lookup_hash(nd);
2202
dentry = lookup_hash(nd);
2203
error = PTR_ERR(dentry);
2204
if (IS_ERR(dentry)) {
2205
mutex_unlock(&dir->d_inode->i_mutex);
2209
path->dentry = dentry;
2308
2210
path->mnt = nd->path.mnt;
2310
error = PTR_ERR(path->dentry);
2311
if (IS_ERR(path->dentry)) {
2312
mutex_unlock(&dir->d_inode->i_mutex);
2316
if (IS_ERR(nd->intent.open.file)) {
2317
error = PTR_ERR(nd->intent.open.file);
2318
goto exit_mutex_unlock;
2321
2212
/* Negative dentry, just create the file */
2322
if (!path->dentry->d_inode) {
2213
if (!dentry->d_inode) {
2214
int mode = op->mode;
2215
if (!IS_POSIXACL(dir->d_inode))
2216
mode &= ~current_umask();
2324
2218
* This write is needed to ensure that a
2325
* ro->rw transition does not occur between
2219
* rw->ro transition does not occur between
2326
2220
* the time when the file is created and when
2327
2221
* a permanent write count is taken through
2328
2222
* the 'struct file' in nameidata_to_filp().
2384
2310
path_put_conditional(path, nd);
2386
path_put(&nd->path);
2387
return ERR_PTR(error);
2312
filp = ERR_PTR(error);
2391
* Note that the low bits of the passed in "open_flag"
2392
* are not the same as in the local variable "flag". See
2393
* open_to_namei_flags() for more details.
2395
struct file *do_filp_open(int dfd, const char *pathname,
2396
int open_flag, int mode, int acc_mode)
2316
static struct file *path_openat(int dfd, const char *pathname,
2317
struct nameidata *nd, const struct open_flags *op, int flags)
2319
struct file *base = NULL;
2398
2320
struct file *filp;
2399
struct nameidata nd;
2403
int flag = open_to_namei_flags(open_flag);
2406
if (!(open_flag & O_CREAT))
2409
/* Must never be set by userspace */
2410
open_flag &= ~FMODE_NONOTIFY;
2413
* O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
2414
* check for O_DSYNC if the need any syncing at all we enforce it's
2415
* always set instead of having to deal with possibly weird behaviour
2416
* for malicious applications setting only __O_SYNC.
2418
if (open_flag & __O_SYNC)
2419
open_flag |= O_DSYNC;
2422
acc_mode = MAY_OPEN | ACC_MODE(open_flag);
2424
/* O_TRUNC implies we need access checks for write permissions */
2425
if (open_flag & O_TRUNC)
2426
acc_mode |= MAY_WRITE;
2428
/* Allow the LSM permission hook to distinguish append
2429
access from general write access. */
2430
if (open_flag & O_APPEND)
2431
acc_mode |= MAY_APPEND;
2433
flags = LOOKUP_OPEN;
2434
if (open_flag & O_CREAT) {
2435
flags |= LOOKUP_CREATE;
2436
if (open_flag & O_EXCL)
2437
flags |= LOOKUP_EXCL;
2439
if (open_flag & O_DIRECTORY)
2440
flags |= LOOKUP_DIRECTORY;
2441
if (!(open_flag & O_NOFOLLOW))
2442
flags |= LOOKUP_FOLLOW;
2444
2324
filp = get_empty_filp();
2446
2326
return ERR_PTR(-ENFILE);
2448
filp->f_flags = open_flag;
2449
nd.intent.open.file = filp;
2450
nd.intent.open.flags = flag;
2451
nd.intent.open.create_mode = mode;
2453
if (open_flag & O_CREAT)
2456
/* !O_CREAT, simple open */
2457
error = do_path_lookup(dfd, pathname, flags, &nd);
2458
if (unlikely(error))
2461
if (!(nd.flags & LOOKUP_FOLLOW)) {
2462
if (nd.inode->i_op->follow_link)
2466
if (nd.flags & LOOKUP_DIRECTORY) {
2467
if (!nd.inode->i_op->lookup)
2470
audit_inode(pathname, nd.path.dentry);
2471
filp = finish_open(&nd, open_flag, acc_mode);
2473
release_open_intent(&nd);
2479
filp = ERR_PTR(error);
2483
/* OK, have to create the file. Find the parent. */
2484
error = path_init_rcu(dfd, pathname,
2485
LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2488
error = path_walk_rcu(pathname, &nd);
2489
path_finish_rcu(&nd);
2490
if (unlikely(error == -ECHILD || error == -ESTALE)) {
2491
/* slower, locked walk */
2492
if (error == -ESTALE) {
2494
flags |= LOOKUP_REVAL;
2496
error = path_init(dfd, pathname,
2497
LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2501
error = path_walk_simple(pathname, &nd);
2503
if (unlikely(error))
2505
if (unlikely(!audit_dummy_context()))
2506
audit_inode(pathname, nd.path.dentry);
2509
* We have the parent and last component.
2512
filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2328
filp->f_flags = op->open_flag;
2329
nd->intent.open.file = filp;
2330
nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2331
nd->intent.open.create_mode = op->mode;
2333
error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2334
if (unlikely(error))
2337
current->total_link_count = 0;
2338
error = link_path_walk(pathname, nd);
2339
if (unlikely(error))
2342
filp = do_last(nd, &path, op, pathname);
2513
2343
while (unlikely(!filp)) { /* trailing symlink */
2514
2344
struct path link = path;
2515
struct inode *linki = link.dentry->d_inode;
2518
if (!(nd.flags & LOOKUP_FOLLOW))
2523
* This is subtle. Instead of calling do_follow_link() we do
2524
* the thing by hands. The reason is that this way we have zero
2525
* link_count and path_walk() (called from ->follow_link)
2526
* honoring LOOKUP_PARENT. After that we have the parent and
2527
* last component, i.e. we are in the same situation as after
2528
* the first path_walk(). Well, almost - if the last component
2529
* is normal we get its copy stored in nd->last.name and we will
2530
* have to putname() it when we are done. Procfs-like symlinks
2531
* just set LAST_BIND.
2533
nd.flags |= LOOKUP_PARENT;
2534
error = security_inode_follow_link(link.dentry, &nd);
2537
error = __do_follow_link(&link, &nd, &cookie);
2538
if (unlikely(error)) {
2539
if (!IS_ERR(cookie) && linki->i_op->put_link)
2540
linki->i_op->put_link(link.dentry, &nd, cookie);
2541
/* nd.path had been dropped */
2346
if (!(nd->flags & LOOKUP_FOLLOW)) {
2347
path_put_conditional(&path, nd);
2348
path_put(&nd->path);
2349
filp = ERR_PTR(-ELOOP);
2545
nd.flags &= ~LOOKUP_PARENT;
2546
filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2547
if (linki->i_op->put_link)
2548
linki->i_op->put_link(link.dentry, &nd, cookie);
2352
nd->flags |= LOOKUP_PARENT;
2353
nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2354
error = follow_link(&link, nd, &cookie);
2355
if (unlikely(error))
2356
filp = ERR_PTR(error);
2358
filp = do_last(nd, &path, op, pathname);
2359
put_link(nd, &link, cookie);
2554
if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
2556
release_open_intent(&nd);
2362
if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2363
path_put(&nd->root);
2366
release_open_intent(nd);
2560
path_put_conditional(&path, &nd);
2564
2370
filp = ERR_PTR(error);
2569
* filp_open - open file and return file pointer
2571
* @filename: path to open
2572
* @flags: open flags as per the open(2) second argument
2573
* @mode: mode for the new file if O_CREAT is set, else ignored
2575
* This is the helper to open a file from kernelspace if you really
2576
* have to. But in generally you should not do this, so please move
2577
* along, nothing to see here..
2579
struct file *filp_open(const char *filename, int flags, int mode)
2581
return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2583
EXPORT_SYMBOL(filp_open);
2374
struct file *do_filp_open(int dfd, const char *pathname,
2375
const struct open_flags *op, int flags)
2377
struct nameidata nd;
2380
filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2381
if (unlikely(filp == ERR_PTR(-ECHILD)))
2382
filp = path_openat(dfd, pathname, &nd, op, flags);
2383
if (unlikely(filp == ERR_PTR(-ESTALE)))
2384
filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2388
struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2389
const char *name, const struct open_flags *op, int flags)
2391
struct nameidata nd;
2395
nd.root.dentry = dentry;
2397
flags |= LOOKUP_ROOT;
2399
if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2400
return ERR_PTR(-ELOOP);
2402
file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2403
if (unlikely(file == ERR_PTR(-ECHILD)))
2404
file = path_openat(-1, name, &nd, op, flags);
2405
if (unlikely(file == ERR_PTR(-ESTALE)))
2406
file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2586
2411
* lookup_create - lookup a dentry, creating it if it doesn't exist