@@ -2383,6 +2383,125 @@ static noinline int btrfs_ioctl_ino_lookup(struct file *file,
23832383 return ret ;
23842384}
23852385
2386+ /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2387+ static int btrfs_ioctl_get_subvol_info (struct file * file , void __user * argp )
2388+ {
2389+ struct btrfs_ioctl_get_subvol_info_args * subvol_info ;
2390+ struct btrfs_fs_info * fs_info ;
2391+ struct btrfs_root * root ;
2392+ struct btrfs_path * path ;
2393+ struct btrfs_key key ;
2394+ struct btrfs_root_item * root_item ;
2395+ struct btrfs_root_ref * rref ;
2396+ struct extent_buffer * leaf ;
2397+ unsigned long item_off ;
2398+ unsigned long item_len ;
2399+ struct inode * inode ;
2400+ int slot ;
2401+ int ret = 0 ;
2402+
2403+ path = btrfs_alloc_path ();
2404+ if (!path )
2405+ return - ENOMEM ;
2406+
2407+ subvol_info = kzalloc (sizeof (* subvol_info ), GFP_KERNEL );
2408+ if (!subvol_info ) {
2409+ btrfs_free_path (path );
2410+ return - ENOMEM ;
2411+ }
2412+
2413+ inode = file_inode (file );
2414+ fs_info = BTRFS_I (inode )-> root -> fs_info ;
2415+
2416+ /* Get root_item of inode's subvolume */
2417+ key .objectid = BTRFS_I (inode )-> root -> root_key .objectid ;
2418+ key .type = BTRFS_ROOT_ITEM_KEY ;
2419+ key .offset = (u64 )- 1 ;
2420+ root = btrfs_read_fs_root_no_name (fs_info , & key );
2421+ if (IS_ERR (root )) {
2422+ ret = PTR_ERR (root );
2423+ goto out ;
2424+ }
2425+ root_item = & root -> root_item ;
2426+
2427+ subvol_info -> treeid = key .objectid ;
2428+
2429+ subvol_info -> generation = btrfs_root_generation (root_item );
2430+ subvol_info -> flags = btrfs_root_flags (root_item );
2431+
2432+ memcpy (subvol_info -> uuid , root_item -> uuid , BTRFS_UUID_SIZE );
2433+ memcpy (subvol_info -> parent_uuid , root_item -> parent_uuid ,
2434+ BTRFS_UUID_SIZE );
2435+ memcpy (subvol_info -> received_uuid , root_item -> received_uuid ,
2436+ BTRFS_UUID_SIZE );
2437+
2438+ subvol_info -> ctransid = btrfs_root_ctransid (root_item );
2439+ subvol_info -> ctime .sec = btrfs_stack_timespec_sec (& root_item -> ctime );
2440+ subvol_info -> ctime .nsec = btrfs_stack_timespec_nsec (& root_item -> ctime );
2441+
2442+ subvol_info -> otransid = btrfs_root_otransid (root_item );
2443+ subvol_info -> otime .sec = btrfs_stack_timespec_sec (& root_item -> otime );
2444+ subvol_info -> otime .nsec = btrfs_stack_timespec_nsec (& root_item -> otime );
2445+
2446+ subvol_info -> stransid = btrfs_root_stransid (root_item );
2447+ subvol_info -> stime .sec = btrfs_stack_timespec_sec (& root_item -> stime );
2448+ subvol_info -> stime .nsec = btrfs_stack_timespec_nsec (& root_item -> stime );
2449+
2450+ subvol_info -> rtransid = btrfs_root_rtransid (root_item );
2451+ subvol_info -> rtime .sec = btrfs_stack_timespec_sec (& root_item -> rtime );
2452+ subvol_info -> rtime .nsec = btrfs_stack_timespec_nsec (& root_item -> rtime );
2453+
2454+ if (key .objectid != BTRFS_FS_TREE_OBJECTID ) {
2455+ /* Search root tree for ROOT_BACKREF of this subvolume */
2456+ root = fs_info -> tree_root ;
2457+
2458+ key .type = BTRFS_ROOT_BACKREF_KEY ;
2459+ key .offset = 0 ;
2460+ ret = btrfs_search_slot (NULL , root , & key , path , 0 , 0 );
2461+ if (ret < 0 ) {
2462+ goto out ;
2463+ } else if (path -> slots [0 ] >=
2464+ btrfs_header_nritems (path -> nodes [0 ])) {
2465+ ret = btrfs_next_leaf (root , path );
2466+ if (ret < 0 ) {
2467+ goto out ;
2468+ } else if (ret > 0 ) {
2469+ ret = - EUCLEAN ;
2470+ goto out ;
2471+ }
2472+ }
2473+
2474+ leaf = path -> nodes [0 ];
2475+ slot = path -> slots [0 ];
2476+ btrfs_item_key_to_cpu (leaf , & key , slot );
2477+ if (key .objectid == subvol_info -> treeid &&
2478+ key .type == BTRFS_ROOT_BACKREF_KEY ) {
2479+ subvol_info -> parent_id = key .offset ;
2480+
2481+ rref = btrfs_item_ptr (leaf , slot , struct btrfs_root_ref );
2482+ subvol_info -> dirid = btrfs_root_ref_dirid (leaf , rref );
2483+
2484+ item_off = btrfs_item_ptr_offset (leaf , slot )
2485+ + sizeof (struct btrfs_root_ref );
2486+ item_len = btrfs_item_size_nr (leaf , slot )
2487+ - sizeof (struct btrfs_root_ref );
2488+ read_extent_buffer (leaf , subvol_info -> name ,
2489+ item_off , item_len );
2490+ } else {
2491+ ret = - ENOENT ;
2492+ goto out ;
2493+ }
2494+ }
2495+
2496+ if (copy_to_user (argp , subvol_info , sizeof (* subvol_info )))
2497+ ret = - EFAULT ;
2498+
2499+ out :
2500+ btrfs_free_path (path );
2501+ kzfree (subvol_info );
2502+ return ret ;
2503+ }
2504+
23862505static noinline int btrfs_ioctl_snap_destroy (struct file * file ,
23872506 void __user * arg )
23882507{
@@ -5545,6 +5664,8 @@ long btrfs_ioctl(struct file *file, unsigned int
55455664 return btrfs_ioctl_fsgetxattr (file , argp );
55465665 case FS_IOC_FSSETXATTR :
55475666 return btrfs_ioctl_fssetxattr (file , argp );
5667+ case BTRFS_IOC_GET_SUBVOL_INFO :
5668+ return btrfs_ioctl_get_subvol_info (file , argp );
55485669 }
55495670
55505671 return - ENOTTY ;
0 commit comments