File Coverage

blib/lib/Filesys/POSIX/Mount.pm
Criterion Covered Total %
statement 42 42 100.0
branch 4 4 100.0
condition 1 3 33.3
subroutine 12 12 100.0
pod 5 5 100.0
total 64 66 96.9


line stmt bran cond sub pod time code
1             # Copyright (c) 2014, cPanel, Inc.
2             # All rights reserved.
3             # http://cpanel.net/
4             #
5             # This is free software; you can redistribute it and/or modify it under the same
6             # terms as Perl itself. See the LICENSE file for further details.
7              
8             package Filesys::POSIX::Mount;
9              
10 24     24   105 use strict;
  24         26  
  24         1356  
11 24     24   776 use warnings;
  24         705  
  24         1183  
12              
13 24     24   81 use Filesys::POSIX::Bits;
  24         31  
  24         6287  
14 24     24   2639 use Filesys::POSIX::Module ();
  24         25  
  24         322  
15 24     24   79 use Filesys::POSIX::Path ();
  24         28  
  24         376  
16              
17 24     24   81 use Filesys::POSIX::Error qw(throw);
  24         26  
  24         911  
18              
19 24     24   97 use Carp qw(confess);
  24         30  
  24         10507  
20              
21             my @METHODS = qw(mount unmount statfs fstatfs mountlist);
22              
23             Filesys::POSIX::Module->export_methods( __PACKAGE__, @METHODS );
24              
25             =head1 NAME
26              
27             Filesys::POSIX::Mount - Exposes VFS mounting functionality to L
28              
29             =head1 DESCRIPTION
30              
31             C is a mixin module imported into the L
32             namespace by said module that provides a frontend to the internal VFS. Rather
33             than dealing in terms of mount point vnodes as L does, the
34             system calls provided in this module deal in terms of pathnames.
35              
36             =head1 SYSTEM CALLS
37              
38             =over
39              
40             =item C<$fs-Emount($dev, $path, %opts)>
41              
42             Attach the filesystem device, C<$dev>, to the directory inode specified by
43             C<$path>. The C<%opts> hash can be used to pass mount options to the
44             initialization routines for the device object to be mounted; these options are
45             passed to the C<$dev-Einit()> routine that said filesystem device
46             implements.
47              
48             The filesystem mount record is kept in an ordered list by the VFS, and can be
49             retrieved later using the C<$fs-Estatfs>, or C<$fs-Emountlist> system
50             calls.
51              
52             =cut
53              
54             sub mount {
55 13     13 1 1779 my ( $self, $dev, $path, %opts ) = @_;
56 13         57 my $mountpoint = $self->stat($path);
57 13         41 my $realpath = $self->_find_inode_path($mountpoint);
58              
59 13   33     62 $opts{'fs'} ||= $self;
60              
61 13         44 $dev->init(%opts);
62              
63 11         47 $self->{'vfs'}->mount( $dev, $realpath, $mountpoint, %opts );
64             }
65              
66             =item C<$fs-Eunmount($path)>
67              
68             Attempts to unmount a filesystem mounted at the directory pointed to by
69             C<$path>, performing a number of sanity checks to ensure the safety of the
70             current operation. The following checks are made:
71              
72             =over
73              
74             =item The directory inode is retrieved using C<$fs-Estat>.
75              
76             =item Using Cstatfs>, with the directory inode passed,
77             the VFS is queried to determine if the location given has a filesystem mounted
78             at all. If so, the mount record is kept for reference for the next series of
79             checks.
80              
81             =item The file descriptor table is scanned for open files whose inodes exist on
82             the device found for the mount record queried in the previous step by the VFS.
83             An EBUSY exception is thrown when matching file descriptors are found.
84              
85             =item The current working directory is checked to ensure it is not a reference
86             to a directory inode associated with the mounted device. An EBUSY exception is
87             thrown if the current directory is on the same device that is to be unmounted.
88              
89             =back
90              
91             =cut
92              
93             sub unmount {
94 5     5 1 137 my ( $self, $path ) = @_;
95 5         11 my $mountpoint = $self->stat($path);
96 5         14 my $mount = $self->{'vfs'}->statfs( $mountpoint, 'exact' => 1 );
97              
98             #
99             # First, check for open file descriptors held on the desired device.
100             #
101 5         14 foreach ( $self->{'fds'}->list ) {
102 3         7 my $inode = $self->{'fds'}->lookup($_)->{'inode'};
103              
104 3 100       15 throw &Errno::EBUSY if $mount->{'dev'} eq $inode->{'dev'};
105             }
106              
107             #
108             # Next, check to see if the current working directory's device inode
109             # is the same device as the one being requested for unmounting.
110             #
111 4 100       17 throw &Errno::EBUSY if $mount->{'dev'} eq $self->{'cwd'}->{'dev'};
112              
113 3         8 $self->{'vfs'}->unmount($mount);
114             }
115              
116             =item C<$fs-Estatfs($path)>
117              
118             Returns the mount record for the device associated with the inode specified by
119             $path. The inode is found using C<$fs-Estat>, then queried for by
120             Cstatfs>.
121              
122             =cut
123              
124             sub statfs {
125 3     3 1 1085 my ( $self, $path ) = @_;
126 3         7 my $inode = $self->stat($path);
127              
128 3         10 return $self->{'vfs'}->statfs($inode);
129             }
130              
131             =item C<$fs-Efstatfs($fd)>
132              
133             Returns the mount record for the device associated with the inode referenced by
134             the open file descriptor, C<$fd>. The inode is found using C<$fs-Efstat>,
135             then queried for by Cstatfs>.
136              
137             =cut
138              
139             sub fstatfs {
140 3     3 1 2032 my ( $self, $fd ) = @_;
141 3         9 my $inode = $self->fstat($fd);
142              
143 3         10 return $self->{'vfs'}->statfs($inode);
144             }
145              
146             =item C<$fs-Emountlist>
147              
148             Returns a list of records for each filesystem currently mounted, in the order
149             in which they were mounted.
150              
151             =cut
152              
153             sub mountlist {
154 1     1 1 11 shift->{'vfs'}->mountlist;
155             }
156              
157             =back
158              
159             =head1 ANATOMY OF A MOUNT RECORD
160              
161             Mount records are created internally by Cmount>, and
162             are stored as anonymous HASHes. They contain the following attributes:
163              
164             =over
165              
166             =item C
167              
168             Reference to the directory inode (or vnode in the case of multiple filesystems
169             mounted in the same logical location) the filesystem is mounted to.
170              
171             =item C
172              
173             Reference to the mounted filesystem's root directory inode. This is never a
174             vnode.
175              
176             =item C
177              
178             The value of the C flag specified in a call to C<$fs-Emount>. When
179             no value is specified, the value stored is equal to C.
180              
181             =item C
182              
183             A reference to the filesystem device object that was mounted by
184             C<$fs-Emount>.
185              
186             =item C
187              
188             A lowercase string formed by chopping all but the last item in a Perl fully
189             qualified package name corresponding to the type of the device mounted. For
190             instance, an instance of L mounted will result in a value
191             of C<'mem'>.
192              
193             =item C
194              
195             The true, original, and sanitized path of the mount point specified by
196             C<$fs-Emount>.
197              
198             =item C
199              
200             A VFS inode created by Cnew>, containing most
201             attributes of the mounted device's root inode, but with a parent pointing to
202             the mount point inode's parent.
203              
204             =item C
205              
206             A copy of the options passed to C<$fs-Emount>, minus the C option.
207              
208             =back
209              
210             =cut
211              
212             1;
213              
214             __END__