aboutsummaryrefslogtreecommitdiffstats
path: root/fs/file_table.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/file_table.c')
-rw-r--r--fs/file_table.c64
1 files changed, 57 insertions, 7 deletions
diff --git a/fs/file_table.c b/fs/file_table.c
index eb36b6b17e26..01e4c1e8e6b6 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -190,7 +190,8 @@ struct file *alloc_file(struct path *path, fmode_t mode,
190 file_take_write(file); 190 file_take_write(file);
191 WARN_ON(mnt_clone_write(path->mnt)); 191 WARN_ON(mnt_clone_write(path->mnt));
192 } 192 }
193 ima_counts_get(file); 193 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
194 i_readcount_inc(path->dentry->d_inode);
194 return file; 195 return file;
195} 196}
196EXPORT_SYMBOL(alloc_file); 197EXPORT_SYMBOL(alloc_file);
@@ -246,11 +247,15 @@ static void __fput(struct file *file)
246 file->f_op->release(inode, file); 247 file->f_op->release(inode, file);
247 security_file_free(file); 248 security_file_free(file);
248 ima_file_free(file); 249 ima_file_free(file);
249 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL)) 250 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
251 !(file->f_mode & FMODE_PATH))) {
250 cdev_put(inode->i_cdev); 252 cdev_put(inode->i_cdev);
253 }
251 fops_put(file->f_op); 254 fops_put(file->f_op);
252 put_pid(file->f_owner.pid); 255 put_pid(file->f_owner.pid);
253 file_sb_list_del(file); 256 file_sb_list_del(file);
257 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
258 i_readcount_dec(inode);
254 if (file->f_mode & FMODE_WRITE) 259 if (file->f_mode & FMODE_WRITE)
255 drop_file_write_access(file); 260 drop_file_write_access(file);
256 file->f_path.dentry = NULL; 261 file->f_path.dentry = NULL;
@@ -276,11 +281,10 @@ struct file *fget(unsigned int fd)
276 rcu_read_lock(); 281 rcu_read_lock();
277 file = fcheck_files(files, fd); 282 file = fcheck_files(files, fd);
278 if (file) { 283 if (file) {
279 if (!atomic_long_inc_not_zero(&file->f_count)) { 284 /* File object ref couldn't be taken */
280 /* File object ref couldn't be taken */ 285 if (file->f_mode & FMODE_PATH ||
281 rcu_read_unlock(); 286 !atomic_long_inc_not_zero(&file->f_count))
282 return NULL; 287 file = NULL;
283 }
284 } 288 }
285 rcu_read_unlock(); 289 rcu_read_unlock();
286 290
@@ -289,6 +293,25 @@ struct file *fget(unsigned int fd)
289 293
290EXPORT_SYMBOL(fget); 294EXPORT_SYMBOL(fget);
291 295
296struct file *fget_raw(unsigned int fd)
297{
298 struct file *file;
299 struct files_struct *files = current->files;
300
301 rcu_read_lock();
302 file = fcheck_files(files, fd);
303 if (file) {
304 /* File object ref couldn't be taken */
305 if (!atomic_long_inc_not_zero(&file->f_count))
306 file = NULL;
307 }
308 rcu_read_unlock();
309
310 return file;
311}
312
313EXPORT_SYMBOL(fget_raw);
314
292/* 315/*
293 * Lightweight file lookup - no refcnt increment if fd table isn't shared. 316 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
294 * 317 *
@@ -313,6 +336,33 @@ struct file *fget_light(unsigned int fd, int *fput_needed)
313 *fput_needed = 0; 336 *fput_needed = 0;
314 if (atomic_read(&files->count) == 1) { 337 if (atomic_read(&files->count) == 1) {
315 file = fcheck_files(files, fd); 338 file = fcheck_files(files, fd);
339 if (file && (file->f_mode & FMODE_PATH))
340 file = NULL;
341 } else {
342 rcu_read_lock();
343 file = fcheck_files(files, fd);
344 if (file) {
345 if (!(file->f_mode & FMODE_PATH) &&
346 atomic_long_inc_not_zero(&file->f_count))
347 *fput_needed = 1;
348 else
349 /* Didn't get the reference, someone's freed */
350 file = NULL;
351 }
352 rcu_read_unlock();
353 }
354
355 return file;
356}
357
358struct file *fget_raw_light(unsigned int fd, int *fput_needed)
359{
360 struct file *file;
361 struct files_struct *files = current->files;
362
363 *fput_needed = 0;
364 if (atomic_read(&files->count) == 1) {
365 file = fcheck_files(files, fd);
316 } else { 366 } else {
317 rcu_read_lock(); 367 rcu_read_lock();
318 file = fcheck_files(files, fd); 368 file = fcheck_files(files, fd);