LCOV - code coverage report
Current view: top level - source3/modules - vfs_ceph_snapshots.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 2 621 0.3 %
Date: 2021-09-23 10:06:22 Functions: 1 28 3.6 %

          Line data    Source code
       1             : /*
       2             :  * Module for accessing CephFS snapshots as Previous Versions. This module is
       3             :  * separate to vfs_ceph, so that it can also be used atop a CephFS kernel backed
       4             :  * share with vfs_default.
       5             :  *
       6             :  * Copyright (C) David Disseldorp 2019
       7             :  *
       8             :  * This program is free software; you can redistribute it and/or modify
       9             :  * it under the terms of the GNU General Public License as published by
      10             :  * the Free Software Foundation; either version 3 of the License, or
      11             :  * (at your option) any later version.
      12             :  *
      13             :  * This program is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  * GNU General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU General Public License
      19             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      20             :  */
      21             : 
      22             : #include <dirent.h>
      23             : #include <libgen.h>
      24             : #include "includes.h"
      25             : #include "include/ntioctl.h"
      26             : #include "include/smb.h"
      27             : #include "system/filesys.h"
      28             : #include "smbd/smbd.h"
      29             : #include "lib/util/tevent_ntstatus.h"
      30             : #include "lib/util/smb_strtox.h"
      31             : 
      32             : #undef DBGC_CLASS
      33             : #define DBGC_CLASS DBGC_VFS
      34             : 
      35             : /*
      36             :  * CephFS has a magic snapshots subdirectory in all parts of the directory tree.
      37             :  * This module automatically makes all snapshots in this subdir visible to SMB
      38             :  * clients (if permitted by corresponding access control).
      39             :  */
      40             : #define CEPH_SNAP_SUBDIR_DEFAULT ".snap"
      41             : /*
      42             :  * The ceph.snap.btime (virtual) extended attribute carries the snapshot
      43             :  * creation time in $secs.$nsecs format. It was added as part of
      44             :  * https://tracker.ceph.com/issues/38838. Running Samba atop old Ceph versions
      45             :  * which don't provide this xattr will not be able to enumerate or access
      46             :  * snapshots using this module. As an alternative, vfs_shadow_copy2 could be
      47             :  * used instead, alongside special shadow:format snapshot directory names.
      48             :  */
      49             : #define CEPH_SNAP_BTIME_XATTR "ceph.snap.btime"
      50             : 
      51           0 : static int ceph_snap_get_btime_fsp(struct vfs_handle_struct *handle,
      52             :                                    struct files_struct *fsp,
      53             :                                    time_t *_snap_secs)
      54             : {
      55             :         int ret;
      56             :         char snap_btime[33];
      57           0 :         char *s = NULL;
      58           0 :         char *endptr = NULL;
      59             :         struct timespec snap_timespec;
      60             :         int err;
      61             : 
      62           0 :         ret = SMB_VFS_NEXT_FGETXATTR(handle,
      63             :                                     fsp,
      64             :                                     CEPH_SNAP_BTIME_XATTR,
      65             :                                     snap_btime,
      66             :                                     sizeof(snap_btime));
      67           0 :         if (ret < 0) {
      68           0 :                 DBG_ERR("failed to get %s xattr: %s\n",
      69             :                         CEPH_SNAP_BTIME_XATTR, strerror(errno));
      70           0 :                 return -errno;
      71             :         }
      72             : 
      73           0 :         if (ret == 0 || ret >= sizeof(snap_btime) - 1) {
      74           0 :                 return -EINVAL;
      75             :         }
      76             : 
      77             :         /* ensure zero termination */
      78           0 :         snap_btime[ret] = '\0';
      79             : 
      80             :         /* format is sec.nsec */
      81           0 :         s = strchr(snap_btime, '.');
      82           0 :         if (s == NULL) {
      83           0 :                 DBG_ERR("invalid %s xattr value: %s\n",
      84             :                         CEPH_SNAP_BTIME_XATTR, snap_btime);
      85           0 :                 return -EINVAL;
      86             :         }
      87             : 
      88             :         /* First component is seconds, extract it */
      89           0 :         *s = '\0';
      90           0 :         snap_timespec.tv_sec = smb_strtoull(snap_btime,
      91             :                                             &endptr,
      92             :                                             10,
      93             :                                             &err,
      94             :                                             SMB_STR_FULL_STR_CONV);
      95           0 :         if (err != 0) {
      96           0 :                 return -err;
      97             :         }
      98             : 
      99             :         /* second component is nsecs */
     100           0 :         s++;
     101           0 :         snap_timespec.tv_nsec = smb_strtoul(s,
     102             :                                             &endptr,
     103             :                                             10,
     104             :                                             &err,
     105             :                                             SMB_STR_FULL_STR_CONV);
     106           0 :         if (err != 0) {
     107           0 :                 return -err;
     108             :         }
     109             : 
     110             :         /*
     111             :          * >> 30 is a rough divide by ~10**9. No need to be exact, as @GMT
     112             :          * tokens only offer 1-second resolution (while twrp is nsec).
     113             :          */
     114           0 :         *_snap_secs = snap_timespec.tv_sec + (snap_timespec.tv_nsec >> 30);
     115             : 
     116           0 :         return 0;
     117             : }
     118             : 
     119             : /*
     120             :  * XXX Ceph snapshots can be created with sub-second granularity, which means
     121             :  * that multiple snapshots may be mapped to the same @GMT- label.
     122             :  *
     123             :  * @this_label is a pre-zeroed buffer to be filled with a @GMT label
     124             :  * @return 0 if label successfully filled or -errno on error.
     125             :  */
     126           0 : static int ceph_snap_fill_label(struct vfs_handle_struct *handle,
     127             :                                 TALLOC_CTX *tmp_ctx,
     128             :                                 struct files_struct *dirfsp,
     129             :                                 const char *subdir,
     130             :                                 SHADOW_COPY_LABEL this_label)
     131             : {
     132           0 :         const char *parent_snapsdir = dirfsp->fsp_name->base_name;
     133             :         struct smb_filename *smb_fname;
     134           0 :         struct smb_filename *atname = NULL;
     135             :         time_t snap_secs;
     136             :         struct tm gmt_snap_time;
     137             :         struct tm *tm_ret;
     138             :         size_t str_sz;
     139             :         char snap_path[PATH_MAX + 1];
     140             :         int ret;
     141             :         NTSTATUS status;
     142             : 
     143             :         /*
     144             :          * CephFS snapshot creation times are available via a special
     145             :          * xattr - snapshot b/m/ctimes all match the snap source.
     146             :          */
     147           0 :         ret = snprintf(snap_path, sizeof(snap_path), "%s/%s",
     148             :                         parent_snapsdir, subdir);
     149           0 :         if (ret >= sizeof(snap_path)) {
     150           0 :                 return -EINVAL;
     151             :         }
     152             : 
     153           0 :         smb_fname = synthetic_smb_fname(tmp_ctx,
     154             :                                         snap_path,
     155             :                                         NULL,
     156             :                                         NULL,
     157             :                                         0,
     158             :                                         0);
     159           0 :         if (smb_fname == NULL) {
     160           0 :                 return -ENOMEM;
     161             :         }
     162             : 
     163           0 :         ret = vfs_stat(handle->conn, smb_fname);
     164           0 :         if (ret < 0) {
     165           0 :                 ret = -errno;
     166           0 :                 TALLOC_FREE(smb_fname);
     167           0 :                 return ret;
     168             :         }
     169             : 
     170           0 :         atname = synthetic_smb_fname(tmp_ctx,
     171             :                                      subdir,
     172             :                                      NULL,
     173           0 :                                      &smb_fname->st,
     174             :                                      0,
     175             :                                      0);
     176           0 :         if (atname == NULL) {
     177           0 :                 TALLOC_FREE(smb_fname);
     178           0 :                 return -ENOMEM;
     179             :         }
     180             : 
     181           0 :         status = openat_pathref_fsp(dirfsp, atname);
     182           0 :         if (!NT_STATUS_IS_OK(status)) {
     183           0 :                 TALLOC_FREE(smb_fname);
     184           0 :                 TALLOC_FREE(atname);
     185           0 :                 return -map_errno_from_nt_status(status);
     186             :         }
     187             : 
     188           0 :         ret = ceph_snap_get_btime_fsp(handle, atname->fsp, &snap_secs);
     189           0 :         if (ret < 0) {
     190           0 :                 TALLOC_FREE(smb_fname);
     191           0 :                 TALLOC_FREE(atname);
     192           0 :                 return ret;
     193             :         }
     194           0 :         TALLOC_FREE(smb_fname);
     195           0 :         TALLOC_FREE(atname);
     196             : 
     197           0 :         tm_ret = gmtime_r(&snap_secs, &gmt_snap_time);
     198           0 :         if (tm_ret == NULL) {
     199           0 :                 return -EINVAL;
     200             :         }
     201           0 :         str_sz = strftime(this_label, sizeof(SHADOW_COPY_LABEL),
     202             :                           "@GMT-%Y.%m.%d-%H.%M.%S", &gmt_snap_time);
     203           0 :         if (str_sz == 0) {
     204           0 :                 DBG_ERR("failed to convert tm to @GMT token\n");
     205           0 :                 return -EINVAL;
     206             :         }
     207             : 
     208           0 :         DBG_DEBUG("mapped snapshot at %s to enum snaps label %s\n",
     209             :                   snap_path, this_label);
     210             : 
     211           0 :         return 0;
     212             : }
     213             : 
     214           0 : static int ceph_snap_enum_snapdir(struct vfs_handle_struct *handle,
     215             :                                   struct smb_filename *snaps_dname,
     216             :                                   bool labels,
     217             :                                   struct shadow_copy_data *sc_data)
     218             : {
     219           0 :         TALLOC_CTX *frame = talloc_stackframe();
     220           0 :         struct smb_Dir *dir_hnd = NULL;
     221           0 :         struct files_struct *dirfsp = NULL;
     222           0 :         const char *dname = NULL;
     223           0 :         char *talloced = NULL;
     224           0 :         long offset = 0;
     225             :         NTSTATUS status;
     226             :         int ret;
     227             :         uint32_t slots;
     228             : 
     229           0 :         DBG_DEBUG("enumerating shadow copy dir at %s\n",
     230             :                   snaps_dname->base_name);
     231             : 
     232             :         /*
     233             :          * CephFS stat(dir).size *normally* returns the number of child entries
     234             :          * for a given dir, but it unfortunately that's not the case for the one
     235             :          * place we need it (dir=.snap), so we need to dynamically determine it
     236             :          * via readdir.
     237             :          */
     238             : 
     239           0 :         dir_hnd = OpenDir(frame, handle->conn, snaps_dname, NULL, 0);
     240           0 :         if (dir_hnd == NULL) {
     241           0 :                 ret = -errno;
     242           0 :                 goto err_out;
     243             :         }
     244             : 
     245             :         /* Check we have SEC_DIR_LIST access on this fsp. */
     246           0 :         dirfsp = dir_hnd_fetch_fsp(dir_hnd);
     247           0 :         status = smbd_check_access_rights_fsp(dirfsp->conn->cwd_fsp,
     248             :                                               dirfsp,
     249             :                                               false,
     250             :                                               SEC_DIR_LIST);
     251           0 :         if (!NT_STATUS_IS_OK(status)) {
     252           0 :                 DBG_ERR("user does not have list permission "
     253             :                         "on snapdir %s\n",
     254             :                         fsp_str_dbg(dirfsp));
     255           0 :                 ret = -map_errno_from_nt_status(status);
     256           0 :                 goto err_out;
     257             :         }
     258             : 
     259           0 :         slots = 0;
     260           0 :         sc_data->num_volumes = 0;
     261           0 :         sc_data->labels = NULL;
     262             : 
     263           0 :         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
     264             :                != NULL)
     265             :         {
     266           0 :                 if (ISDOT(dname) || ISDOTDOT(dname)) {
     267           0 :                         TALLOC_FREE(talloced);
     268           0 :                         continue;
     269             :                 }
     270           0 :                 sc_data->num_volumes++;
     271           0 :                 if (!labels) {
     272           0 :                         TALLOC_FREE(talloced);
     273           0 :                         continue;
     274             :                 }
     275           0 :                 if (sc_data->num_volumes > slots) {
     276           0 :                         uint32_t new_slot_count = slots + 10;
     277           0 :                         SMB_ASSERT(new_slot_count > slots);
     278           0 :                         sc_data->labels = talloc_realloc(sc_data,
     279             :                                                          sc_data->labels,
     280             :                                                          SHADOW_COPY_LABEL,
     281             :                                                          new_slot_count);
     282           0 :                         if (sc_data->labels == NULL) {
     283           0 :                                 TALLOC_FREE(talloced);
     284           0 :                                 ret = -ENOMEM;
     285           0 :                                 goto err_closedir;
     286             :                         }
     287           0 :                         memset(sc_data->labels[slots], 0,
     288             :                                sizeof(SHADOW_COPY_LABEL) * 10);
     289             : 
     290           0 :                         DBG_DEBUG("%d->%d slots for enum_snaps response\n",
     291             :                                   slots, new_slot_count);
     292           0 :                         slots = new_slot_count;
     293             :                 }
     294           0 :                 DBG_DEBUG("filling shadow copy label for %s/%s\n",
     295             :                           snaps_dname->base_name, dname);
     296           0 :                 ret = ceph_snap_fill_label(handle,
     297             :                                 snaps_dname,
     298             :                                 dirfsp,
     299             :                                 dname,
     300           0 :                                 sc_data->labels[sc_data->num_volumes - 1]);
     301           0 :                 if (ret < 0) {
     302           0 :                         TALLOC_FREE(talloced);
     303           0 :                         goto err_closedir;
     304             :                 }
     305           0 :                 TALLOC_FREE(talloced);
     306             :         }
     307             : 
     308           0 :         DBG_DEBUG("%s shadow copy enumeration found %d labels \n",
     309             :                   snaps_dname->base_name, sc_data->num_volumes);
     310             : 
     311           0 :         TALLOC_FREE(frame);
     312           0 :         return 0;
     313             : 
     314           0 : err_closedir:
     315           0 :         TALLOC_FREE(frame);
     316           0 : err_out:
     317           0 :         TALLOC_FREE(sc_data->labels);
     318           0 :         return ret;
     319             : }
     320             : 
     321             : /*
     322             :  * Prior reading: The Meaning of Path Names
     323             :  *   https://wiki.samba.org/index.php/Writing_a_Samba_VFS_Module
     324             :  *
     325             :  * translate paths so that we can use the parent dir for .snap access:
     326             :  *   myfile        -> parent=        trimmed=myfile
     327             :  *   /a            -> parent=/       trimmed=a
     328             :  *   dir/sub/file  -> parent=dir/sub trimmed=file
     329             :  *   /dir/sub      -> parent=/dir/   trimmed=sub
     330             :  */
     331           0 : static int ceph_snap_get_parent_path(const char *connectpath,
     332             :                                      const char *path,
     333             :                                      char *_parent_buf,
     334             :                                      size_t buflen,
     335             :                                      const char **_trimmed)
     336             : {
     337             :         const char *p;
     338             :         size_t len;
     339             :         int ret;
     340             : 
     341           0 :         if (!strcmp(path, "/")) {
     342           0 :                 DBG_ERR("can't go past root for %s .snap dir\n", path);
     343           0 :                 return -EINVAL;
     344             :         }
     345             : 
     346           0 :         p = strrchr_m(path, '/'); /* Find final '/', if any */
     347           0 :         if (p == NULL) {
     348           0 :                 DBG_DEBUG("parent .snap dir for %s is cwd\n", path);
     349           0 :                 ret = strlcpy(_parent_buf, "", buflen);
     350           0 :                 if (ret >= buflen) {
     351           0 :                         return -EINVAL;
     352             :                 }
     353           0 :                 if (_trimmed != NULL) {
     354           0 :                         *_trimmed = path;
     355             :                 }
     356           0 :                 return 0;
     357             :         }
     358             : 
     359           0 :         SMB_ASSERT(p >= path);
     360           0 :         len = p - path;
     361             : 
     362           0 :         ret = snprintf(_parent_buf, buflen, "%.*s", (int)len, path);
     363           0 :         if (ret >= buflen) {
     364           0 :                 return -EINVAL;
     365             :         }
     366             : 
     367             :         /* for absolute paths, check that we're not going outside the share */
     368           0 :         if ((len > 0) && (_parent_buf[0] == '/')) {
     369           0 :                 bool connectpath_match = false;
     370           0 :                 size_t clen = strlen(connectpath);
     371           0 :                 DBG_DEBUG("checking absolute path %s lies within share at %s\n",
     372             :                           _parent_buf, connectpath);
     373             :                 /* need to check for separator, to avoid /x/abcd vs /x/ab */
     374           0 :                 connectpath_match = (strncmp(connectpath,
     375             :                                         _parent_buf,
     376             :                                         clen) == 0);
     377           0 :                 if (!connectpath_match
     378           0 :                  || ((_parent_buf[clen] != '/') && (_parent_buf[clen] != '\0'))) {
     379           0 :                         DBG_ERR("%s parent path is outside of share at %s\n",
     380             :                                 _parent_buf, connectpath);
     381           0 :                         return -EINVAL;
     382             :                 }
     383             :         }
     384             : 
     385           0 :         if (_trimmed != NULL) {
     386             :                 /*
     387             :                  * point to path component which was trimmed from _parent_buf
     388             :                  * excluding path separator.
     389             :                  */
     390           0 :                 *_trimmed = p + 1;
     391             :         }
     392             : 
     393           0 :         DBG_DEBUG("generated parent .snap path for %s as %s (trimmed \"%s\")\n",
     394             :                   path, _parent_buf, p + 1);
     395             : 
     396           0 :         return 0;
     397             : }
     398             : 
     399           0 : static int ceph_snap_get_shadow_copy_data(struct vfs_handle_struct *handle,
     400             :                                         struct files_struct *fsp,
     401             :                                         struct shadow_copy_data *sc_data,
     402             :                                         bool labels)
     403             : {
     404             :         int ret;
     405             :         TALLOC_CTX *tmp_ctx;
     406           0 :         const char *parent_dir = NULL;
     407             :         char tmp[PATH_MAX + 1];
     408             :         char snaps_path[PATH_MAX + 1];
     409           0 :         struct smb_filename *snaps_dname = NULL;
     410           0 :         const char *snapdir = lp_parm_const_string(SNUM(handle->conn),
     411             :                                                    "ceph", "snapdir",
     412             :                                                    CEPH_SNAP_SUBDIR_DEFAULT);
     413             : 
     414           0 :         DBG_DEBUG("getting shadow copy data for %s\n",
     415             :                   fsp->fsp_name->base_name);
     416             : 
     417           0 :         tmp_ctx = talloc_new(fsp);
     418           0 :         if (tmp_ctx == NULL) {
     419           0 :                 ret = -ENOMEM;
     420           0 :                 goto err_out;
     421             :         }
     422             : 
     423           0 :         if (sc_data == NULL) {
     424           0 :                 ret = -EINVAL;
     425           0 :                 goto err_out;
     426             :         }
     427             : 
     428           0 :         if (fsp->fsp_flags.is_directory) {
     429           0 :                 parent_dir = fsp->fsp_name->base_name;
     430             :         } else {
     431           0 :                 ret = ceph_snap_get_parent_path(handle->conn->connectpath,
     432           0 :                                                 fsp->fsp_name->base_name,
     433             :                                                 tmp,
     434             :                                                 sizeof(tmp),
     435             :                                                 NULL);  /* trimmed */
     436           0 :                 if (ret < 0) {
     437           0 :                         goto err_out;
     438             :                 }
     439           0 :                 parent_dir = tmp;
     440             :         }
     441             : 
     442           0 :         if (strlen(parent_dir) == 0) {
     443           0 :                 ret = strlcpy(snaps_path, snapdir, sizeof(snaps_path));
     444             :         } else {
     445           0 :                 ret = snprintf(snaps_path, sizeof(snaps_path), "%s/%s",
     446             :                                parent_dir, snapdir);
     447             :         }
     448           0 :         if (ret >= sizeof(snaps_path)) {
     449           0 :                 ret = -EINVAL;
     450           0 :                 goto err_out;
     451             :         }
     452             : 
     453           0 :         snaps_dname = synthetic_smb_fname(tmp_ctx,
     454             :                                 snaps_path,
     455             :                                 NULL,
     456             :                                 NULL,
     457             :                                 0,
     458           0 :                                 fsp->fsp_name->flags);
     459           0 :         if (snaps_dname == NULL) {
     460           0 :                 ret = -ENOMEM;
     461           0 :                 goto err_out;
     462             :         }
     463             : 
     464           0 :         ret = ceph_snap_enum_snapdir(handle, snaps_dname, labels, sc_data);
     465           0 :         if (ret < 0) {
     466           0 :                 goto err_out;
     467             :         }
     468             : 
     469           0 :         talloc_free(tmp_ctx);
     470           0 :         return 0;
     471             : 
     472           0 : err_out:
     473           0 :         talloc_free(tmp_ctx);
     474           0 :         errno = -ret;
     475           0 :         return -1;
     476             : }
     477             : 
     478           0 : static int ceph_snap_gmt_strip_snapshot(struct vfs_handle_struct *handle,
     479             :                                          const struct smb_filename *smb_fname,
     480             :                                          time_t *_timestamp,
     481             :                                          char *_stripped_buf,
     482             :                                          size_t buflen)
     483             : {
     484             :         size_t len;
     485             : 
     486           0 :         if (smb_fname->twrp == 0) {
     487           0 :                 goto no_snapshot;
     488             :         }
     489             : 
     490           0 :         if (_stripped_buf != NULL) {
     491           0 :                 len = strlcpy(_stripped_buf, smb_fname->base_name, buflen);
     492           0 :                 if (len >= buflen) {
     493           0 :                         return -ENAMETOOLONG;
     494             :                 }
     495             :         }
     496             : 
     497           0 :         *_timestamp = nt_time_to_unix(smb_fname->twrp);
     498           0 :         return 0;
     499           0 : no_snapshot:
     500           0 :         *_timestamp = 0;
     501           0 :         return 0;
     502             : }
     503             : 
     504           0 : static int ceph_snap_gmt_convert_dir(struct vfs_handle_struct *handle,
     505             :                                      const char *name,
     506             :                                      time_t timestamp,
     507             :                                      char *_converted_buf,
     508             :                                      size_t buflen)
     509             : {
     510             :         int ret;
     511             :         NTSTATUS status;
     512           0 :         struct smb_Dir *dir_hnd = NULL;
     513           0 :         struct files_struct *dirfsp = NULL;
     514           0 :         const char *dname = NULL;
     515           0 :         char *talloced = NULL;
     516           0 :         long offset = 0;
     517           0 :         struct smb_filename *snaps_dname = NULL;
     518           0 :         const char *snapdir = lp_parm_const_string(SNUM(handle->conn),
     519             :                                                    "ceph", "snapdir",
     520             :                                                    CEPH_SNAP_SUBDIR_DEFAULT);
     521           0 :         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
     522             : 
     523           0 :         if (tmp_ctx == NULL) {
     524           0 :                 ret = -ENOMEM;
     525           0 :                 goto err_out;
     526             :         }
     527             : 
     528             :         /*
     529             :          * Temporally use the caller's return buffer for this.
     530             :          */
     531           0 :         if (strlen(name) == 0) {
     532           0 :                 ret = strlcpy(_converted_buf, snapdir, buflen);
     533             :         } else {
     534           0 :                 ret = snprintf(_converted_buf, buflen, "%s/%s", name, snapdir);
     535             :         }
     536           0 :         if (ret >= buflen) {
     537           0 :                 ret = -EINVAL;
     538           0 :                 goto err_out;
     539             :         }
     540             : 
     541           0 :         snaps_dname = synthetic_smb_fname(tmp_ctx,
     542             :                                 _converted_buf,
     543             :                                 NULL,
     544             :                                 NULL,
     545             :                                 0,
     546             :                                 0);     /* XXX check? */
     547           0 :         if (snaps_dname == NULL) {
     548           0 :                 ret = -ENOMEM;
     549           0 :                 goto err_out;
     550             :         }
     551             : 
     552             :         /* stat first to trigger error fallback in ceph_snap_gmt_convert() */
     553           0 :         ret = SMB_VFS_NEXT_STAT(handle, snaps_dname);
     554           0 :         if (ret < 0) {
     555           0 :                 ret = -errno;
     556           0 :                 goto err_out;
     557             :         }
     558             : 
     559           0 :         DBG_DEBUG("enumerating shadow copy dir at %s\n",
     560             :                   snaps_dname->base_name);
     561             : 
     562           0 :         dir_hnd = OpenDir(tmp_ctx, handle->conn, snaps_dname, NULL, 0);
     563           0 :         if (dir_hnd == NULL) {
     564           0 :                 ret = -errno;
     565           0 :                 goto err_out;
     566             :         }
     567             : 
     568             :         /* Check we have SEC_DIR_LIST access on this fsp. */
     569           0 :         dirfsp = dir_hnd_fetch_fsp(dir_hnd);
     570           0 :         status = smbd_check_access_rights_fsp(dirfsp->conn->cwd_fsp,
     571             :                                               dirfsp,
     572             :                                               false,
     573             :                                               SEC_DIR_LIST);
     574           0 :         if (!NT_STATUS_IS_OK(status)) {
     575           0 :                 DBG_ERR("user does not have list permission "
     576             :                         "on snapdir %s\n",
     577             :                         fsp_str_dbg(dirfsp));
     578           0 :                 ret = -map_errno_from_nt_status(status);
     579           0 :                 goto err_out;
     580             :         }
     581             : 
     582           0 :         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
     583             :                != NULL)
     584             :         {
     585           0 :                 struct smb_filename *smb_fname = NULL;
     586           0 :                 struct smb_filename *atname = NULL;
     587           0 :                 time_t snap_secs = 0;
     588             : 
     589           0 :                 if (ISDOT(dname) || ISDOTDOT(dname)) {
     590           0 :                         TALLOC_FREE(talloced);
     591           0 :                         continue;
     592             :                 }
     593             : 
     594           0 :                 ret = snprintf(_converted_buf, buflen, "%s/%s",
     595             :                                snaps_dname->base_name, dname);
     596           0 :                 if (ret >= buflen) {
     597           0 :                         ret = -EINVAL;
     598           0 :                         goto err_out;
     599             :                 }
     600             : 
     601           0 :                 smb_fname = synthetic_smb_fname(tmp_ctx,
     602             :                                                 _converted_buf,
     603             :                                                 NULL,
     604             :                                                 NULL,
     605             :                                                 0,
     606             :                                                 0);
     607           0 :                 if (smb_fname == NULL) {
     608           0 :                         ret = -ENOMEM;
     609           0 :                         goto err_out;
     610             :                 }
     611             : 
     612           0 :                 ret = vfs_stat(handle->conn, smb_fname);
     613           0 :                 if (ret < 0) {
     614           0 :                         ret = -errno;
     615           0 :                         TALLOC_FREE(smb_fname);
     616           0 :                         goto err_out;
     617             :                 }
     618             : 
     619           0 :                 atname = synthetic_smb_fname(tmp_ctx,
     620             :                                              dname,
     621             :                                              NULL,
     622           0 :                                              &smb_fname->st,
     623             :                                              0,
     624             :                                              0);
     625           0 :                 if (atname == NULL) {
     626           0 :                         TALLOC_FREE(smb_fname);
     627           0 :                         ret = -ENOMEM;
     628           0 :                         goto err_out;
     629             :                 }
     630             : 
     631           0 :                 status = openat_pathref_fsp(dirfsp, atname);
     632           0 :                 if (!NT_STATUS_IS_OK(status)) {
     633           0 :                         TALLOC_FREE(smb_fname);
     634           0 :                         TALLOC_FREE(atname);
     635           0 :                         ret = -map_errno_from_nt_status(status);
     636           0 :                         goto err_out;
     637             :                 }
     638             : 
     639           0 :                 ret = ceph_snap_get_btime_fsp(handle, atname->fsp, &snap_secs);
     640           0 :                 if (ret < 0) {
     641           0 :                         TALLOC_FREE(smb_fname);
     642           0 :                         TALLOC_FREE(atname);
     643           0 :                         goto err_out;
     644             :                 }
     645             : 
     646           0 :                 TALLOC_FREE(smb_fname);
     647           0 :                 TALLOC_FREE(atname);
     648             : 
     649             :                 /*
     650             :                  * check gmt_snap_time matches @timestamp
     651             :                  */
     652           0 :                 if (timestamp == snap_secs) {
     653           0 :                         break;
     654             :                 }
     655           0 :                 DBG_DEBUG("[connectpath %s] %s@%lld no match for snap %s@%lld\n",
     656             :                           handle->conn->connectpath, name, (long long)timestamp,
     657             :                           dname, (long long)snap_secs);
     658           0 :                 TALLOC_FREE(talloced);
     659             :         }
     660             : 
     661           0 :         if (dname == NULL) {
     662           0 :                 DBG_INFO("[connectpath %s] failed to find %s @ time %lld\n",
     663             :                          handle->conn->connectpath, name, (long long)timestamp);
     664           0 :                 ret = -ENOENT;
     665           0 :                 goto err_out;
     666             :         }
     667             : 
     668             :         /* found, _converted_buf already contains path of interest */
     669           0 :         DBG_DEBUG("[connectpath %s] converted %s @ time %lld to %s\n",
     670             :                   handle->conn->connectpath, name, (long long)timestamp,
     671             :                   _converted_buf);
     672             : 
     673           0 :         TALLOC_FREE(talloced);
     674           0 :         talloc_free(tmp_ctx);
     675           0 :         return 0;
     676             : 
     677           0 : err_out:
     678           0 :         TALLOC_FREE(talloced);
     679           0 :         talloc_free(tmp_ctx);
     680           0 :         return ret;
     681             : }
     682             : 
     683           0 : static int ceph_snap_gmt_convert(struct vfs_handle_struct *handle,
     684             :                                      const char *name,
     685             :                                      time_t timestamp,
     686             :                                      char *_converted_buf,
     687             :                                      size_t buflen)
     688             : {
     689             :         int ret;
     690             :         char parent[PATH_MAX + 1];
     691           0 :         const char *trimmed = NULL;
     692             :         /*
     693             :          * CephFS Snapshots for a given dir are nested under the ./.snap subdir
     694             :          * *or* under ../.snap/dir (and subsequent parent dirs).
     695             :          * Child dirs inherit snapshots created in parent dirs if the child
     696             :          * exists at the time of snapshot creation.
     697             :          *
     698             :          * At this point we don't know whether @name refers to a file or dir, so
     699             :          * first assume it's a dir (with a corresponding .snaps subdir)
     700             :          */
     701           0 :         ret = ceph_snap_gmt_convert_dir(handle,
     702             :                                         name,
     703             :                                         timestamp,
     704             :                                         _converted_buf,
     705             :                                         buflen);
     706           0 :         if (ret >= 0) {
     707             :                 /* all done: .snap subdir exists - @name is a dir */
     708           0 :                 DBG_DEBUG("%s is a dir, accessing snaps via .snap\n", name);
     709           0 :                 return ret;
     710             :         }
     711             : 
     712             :         /* @name/.snap access failed, attempt snapshot access via parent */
     713           0 :         DBG_DEBUG("%s/.snap access failed, attempting parent access\n",
     714             :                   name);
     715             : 
     716           0 :         ret = ceph_snap_get_parent_path(handle->conn->connectpath,
     717             :                                         name,
     718             :                                         parent,
     719             :                                         sizeof(parent),
     720             :                                         &trimmed);
     721           0 :         if (ret < 0) {
     722           0 :                 return ret;
     723             :         }
     724             : 
     725           0 :         ret = ceph_snap_gmt_convert_dir(handle,
     726             :                                         parent,
     727             :                                         timestamp,
     728             :                                         _converted_buf,
     729             :                                         buflen);
     730           0 :         if (ret < 0) {
     731           0 :                 return ret;
     732             :         }
     733             : 
     734             :         /*
     735             :          * found snapshot via parent. Append the child path component
     736             :          * that was trimmed... +1 for path separator + 1 for null termination.
     737             :          */
     738           0 :         if (strlen(_converted_buf) + 1 + strlen(trimmed) + 1 > buflen) {
     739           0 :                 return -EINVAL;
     740             :         }
     741           0 :         strlcat(_converted_buf, "/", buflen);
     742           0 :         strlcat(_converted_buf, trimmed, buflen);
     743             : 
     744           0 :         return 0;
     745             : }
     746             : 
     747           0 : static int ceph_snap_gmt_renameat(vfs_handle_struct *handle,
     748             :                         files_struct *srcfsp,
     749             :                         const struct smb_filename *smb_fname_src,
     750             :                         files_struct *dstfsp,
     751             :                         const struct smb_filename *smb_fname_dst)
     752             : {
     753             :         int ret;
     754             :         time_t timestamp_src, timestamp_dst;
     755             : 
     756           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     757             :                                         smb_fname_src,
     758             :                                         &timestamp_src, NULL, 0);
     759           0 :         if (ret < 0) {
     760           0 :                 errno = -ret;
     761           0 :                 return -1;
     762             :         }
     763           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     764             :                                         smb_fname_dst,
     765             :                                         &timestamp_dst, NULL, 0);
     766           0 :         if (ret < 0) {
     767           0 :                 errno = -ret;
     768           0 :                 return -1;
     769             :         }
     770           0 :         if (timestamp_src != 0) {
     771           0 :                 errno = EXDEV;
     772           0 :                 return -1;
     773             :         }
     774           0 :         if (timestamp_dst != 0) {
     775           0 :                 errno = EROFS;
     776           0 :                 return -1;
     777             :         }
     778           0 :         return SMB_VFS_NEXT_RENAMEAT(handle,
     779             :                                 srcfsp,
     780             :                                 smb_fname_src,
     781             :                                 dstfsp,
     782             :                                 smb_fname_dst);
     783             : }
     784             : 
     785             : /* block links from writeable shares to snapshots for now, like other modules */
     786           0 : static int ceph_snap_gmt_symlinkat(vfs_handle_struct *handle,
     787             :                                 const struct smb_filename *link_contents,
     788             :                                 struct files_struct *dirfsp,
     789             :                                 const struct smb_filename *new_smb_fname)
     790             : {
     791             :         int ret;
     792           0 :         time_t timestamp_old = 0;
     793           0 :         time_t timestamp_new = 0;
     794             : 
     795           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     796             :                                 link_contents,
     797             :                                 &timestamp_old,
     798             :                                 NULL, 0);
     799           0 :         if (ret < 0) {
     800           0 :                 errno = -ret;
     801           0 :                 return -1;
     802             :         }
     803           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     804             :                                 new_smb_fname,
     805             :                                 &timestamp_new,
     806             :                                 NULL, 0);
     807           0 :         if (ret < 0) {
     808           0 :                 errno = -ret;
     809           0 :                 return -1;
     810             :         }
     811           0 :         if ((timestamp_old != 0) || (timestamp_new != 0)) {
     812           0 :                 errno = EROFS;
     813           0 :                 return -1;
     814             :         }
     815           0 :         return SMB_VFS_NEXT_SYMLINKAT(handle,
     816             :                                 link_contents,
     817             :                                 dirfsp,
     818             :                                 new_smb_fname);
     819             : }
     820             : 
     821           0 : static int ceph_snap_gmt_linkat(vfs_handle_struct *handle,
     822             :                                 files_struct *srcfsp,
     823             :                                 const struct smb_filename *old_smb_fname,
     824             :                                 files_struct *dstfsp,
     825             :                                 const struct smb_filename *new_smb_fname,
     826             :                                 int flags)
     827             : {
     828             :         int ret;
     829           0 :         time_t timestamp_old = 0;
     830           0 :         time_t timestamp_new = 0;
     831             : 
     832           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     833             :                                 old_smb_fname,
     834             :                                 &timestamp_old,
     835             :                                 NULL, 0);
     836           0 :         if (ret < 0) {
     837           0 :                 errno = -ret;
     838           0 :                 return -1;
     839             :         }
     840           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     841             :                                 new_smb_fname,
     842             :                                 &timestamp_new,
     843             :                                 NULL, 0);
     844           0 :         if (ret < 0) {
     845           0 :                 errno = -ret;
     846           0 :                 return -1;
     847             :         }
     848           0 :         if ((timestamp_old != 0) || (timestamp_new != 0)) {
     849           0 :                 errno = EROFS;
     850           0 :                 return -1;
     851             :         }
     852           0 :         return SMB_VFS_NEXT_LINKAT(handle,
     853             :                         srcfsp,
     854             :                         old_smb_fname,
     855             :                         dstfsp,
     856             :                         new_smb_fname,
     857             :                         flags);
     858             : }
     859             : 
     860           0 : static int ceph_snap_gmt_stat(vfs_handle_struct *handle,
     861             :                             struct smb_filename *smb_fname)
     862             : {
     863           0 :         time_t timestamp = 0;
     864             :         char stripped[PATH_MAX + 1];
     865             :         char conv[PATH_MAX + 1];
     866             :         char *tmp;
     867             :         int ret;
     868             : 
     869           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     870             :                                         smb_fname,
     871             :                                         &timestamp, stripped, sizeof(stripped));
     872           0 :         if (ret < 0) {
     873           0 :                 errno = -ret;
     874           0 :                 return -1;
     875             :         }
     876           0 :         if (timestamp == 0) {
     877           0 :                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
     878             :         }
     879             : 
     880           0 :         ret = ceph_snap_gmt_convert(handle, stripped,
     881             :                                         timestamp, conv, sizeof(conv));
     882           0 :         if (ret < 0) {
     883           0 :                 errno = -ret;
     884           0 :                 return -1;
     885             :         }
     886           0 :         tmp = smb_fname->base_name;
     887           0 :         smb_fname->base_name = conv;
     888             : 
     889           0 :         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
     890           0 :         smb_fname->base_name = tmp;
     891           0 :         return ret;
     892             : }
     893             : 
     894           0 : static int ceph_snap_gmt_lstat(vfs_handle_struct *handle,
     895             :                              struct smb_filename *smb_fname)
     896             : {
     897           0 :         time_t timestamp = 0;
     898             :         char stripped[PATH_MAX + 1];
     899             :         char conv[PATH_MAX + 1];
     900             :         char *tmp;
     901             :         int ret;
     902             : 
     903           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     904             :                                         smb_fname,
     905             :                                         &timestamp, stripped, sizeof(stripped));
     906           0 :         if (ret < 0) {
     907           0 :                 errno = -ret;
     908           0 :                 return -1;
     909             :         }
     910           0 :         if (timestamp == 0) {
     911           0 :                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
     912             :         }
     913             : 
     914           0 :         ret = ceph_snap_gmt_convert(handle, stripped,
     915             :                                         timestamp, conv, sizeof(conv));
     916           0 :         if (ret < 0) {
     917           0 :                 errno = -ret;
     918           0 :                 return -1;
     919             :         }
     920           0 :         tmp = smb_fname->base_name;
     921           0 :         smb_fname->base_name = conv;
     922             : 
     923           0 :         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
     924           0 :         smb_fname->base_name = tmp;
     925           0 :         return ret;
     926             : }
     927             : 
     928           0 : static int ceph_snap_gmt_openat(vfs_handle_struct *handle,
     929             :                                 const struct files_struct *dirfsp,
     930             :                                 const struct smb_filename *smb_fname_in,
     931             :                                 files_struct *fsp,
     932             :                                 int flags,
     933             :                                 mode_t mode)
     934             : {
     935           0 :         time_t timestamp = 0;
     936           0 :         struct smb_filename *smb_fname = NULL;
     937             :         char stripped[PATH_MAX + 1];
     938             :         char conv[PATH_MAX + 1];
     939             :         int ret;
     940           0 :         int saved_errno = 0;
     941             : 
     942           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
     943             :                                            smb_fname_in,
     944             :                                            &timestamp,
     945             :                                            stripped,
     946             :                                            sizeof(stripped));
     947           0 :         if (ret < 0) {
     948           0 :                 errno = -ret;
     949           0 :                 return -1;
     950             :         }
     951           0 :         if (timestamp == 0) {
     952           0 :                 return SMB_VFS_NEXT_OPENAT(handle,
     953             :                                            dirfsp,
     954             :                                            smb_fname_in,
     955             :                                            fsp,
     956             :                                            flags,
     957             :                                            mode);
     958             :         }
     959             : 
     960           0 :         ret = ceph_snap_gmt_convert(handle,
     961             :                                     stripped,
     962             :                                     timestamp,
     963             :                                     conv,
     964             :                                     sizeof(conv));
     965           0 :         if (ret < 0) {
     966           0 :                 errno = -ret;
     967           0 :                 return -1;
     968             :         }
     969           0 :         smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
     970           0 :         if (smb_fname == NULL) {
     971           0 :                 return -1;
     972             :         }
     973           0 :         smb_fname->base_name = conv;
     974             : 
     975           0 :         ret = SMB_VFS_NEXT_OPENAT(handle,
     976             :                                   dirfsp,
     977             :                                   smb_fname,
     978             :                                   fsp,
     979             :                                   flags,
     980             :                                   mode);
     981           0 :         if (ret == -1) {
     982           0 :                 saved_errno = errno;
     983             :         }
     984           0 :         TALLOC_FREE(smb_fname);
     985           0 :         if (saved_errno != 0) {
     986           0 :                 errno = saved_errno;
     987             :         }
     988           0 :         return ret;
     989             : }
     990             : 
     991           0 : static int ceph_snap_gmt_unlinkat(vfs_handle_struct *handle,
     992             :                         struct files_struct *dirfsp,
     993             :                         const struct smb_filename *csmb_fname,
     994             :                         int flags)
     995             : {
     996           0 :         time_t timestamp = 0;
     997             :         int ret;
     998             : 
     999           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1000             :                                         csmb_fname,
    1001             :                                         &timestamp, NULL, 0);
    1002           0 :         if (ret < 0) {
    1003           0 :                 errno = -ret;
    1004           0 :                 return -1;
    1005             :         }
    1006           0 :         if (timestamp != 0) {
    1007           0 :                 errno = EROFS;
    1008           0 :                 return -1;
    1009             :         }
    1010           0 :         return SMB_VFS_NEXT_UNLINKAT(handle,
    1011             :                         dirfsp,
    1012             :                         csmb_fname,
    1013             :                         flags);
    1014             : }
    1015             : 
    1016           0 : static int ceph_snap_gmt_fchmod(vfs_handle_struct *handle,
    1017             :                                 struct files_struct *fsp,
    1018             :                                 mode_t mode)
    1019             : {
    1020           0 :         const struct smb_filename *csmb_fname = NULL;
    1021           0 :         time_t timestamp = 0;
    1022             :         int ret;
    1023             : 
    1024           0 :         csmb_fname = fsp->fsp_name;
    1025           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1026             :                                         csmb_fname,
    1027             :                                         &timestamp, NULL, 0);
    1028           0 :         if (ret < 0) {
    1029           0 :                 errno = -ret;
    1030           0 :                 return -1;
    1031             :         }
    1032           0 :         if (timestamp != 0) {
    1033           0 :                 errno = EROFS;
    1034           0 :                 return -1;
    1035             :         }
    1036           0 :         return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
    1037             : }
    1038             : 
    1039           0 : static int ceph_snap_gmt_chdir(vfs_handle_struct *handle,
    1040             :                         const struct smb_filename *csmb_fname)
    1041             : {
    1042           0 :         time_t timestamp = 0;
    1043             :         char stripped[PATH_MAX + 1];
    1044             :         char conv[PATH_MAX + 1];
    1045             :         int ret;
    1046             :         struct smb_filename *new_fname;
    1047             :         int saved_errno;
    1048             : 
    1049           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1050             :                                         csmb_fname,
    1051             :                                         &timestamp, stripped, sizeof(stripped));
    1052           0 :         if (ret < 0) {
    1053           0 :                 errno = -ret;
    1054           0 :                 return -1;
    1055             :         }
    1056           0 :         if (timestamp == 0) {
    1057           0 :                 return SMB_VFS_NEXT_CHDIR(handle, csmb_fname);
    1058             :         }
    1059             : 
    1060           0 :         ret = ceph_snap_gmt_convert_dir(handle, stripped,
    1061             :                                         timestamp, conv, sizeof(conv));
    1062           0 :         if (ret < 0) {
    1063           0 :                 errno = -ret;
    1064           0 :                 return -1;
    1065             :         }
    1066           0 :         new_fname = cp_smb_filename(talloc_tos(), csmb_fname);
    1067           0 :         if (new_fname == NULL) {
    1068           0 :                 errno = ENOMEM;
    1069           0 :                 return -1;
    1070             :         }
    1071           0 :         new_fname->base_name = conv;
    1072             : 
    1073           0 :         ret = SMB_VFS_NEXT_CHDIR(handle, new_fname);
    1074           0 :         saved_errno = errno;
    1075           0 :         TALLOC_FREE(new_fname);
    1076           0 :         errno = saved_errno;
    1077           0 :         return ret;
    1078             : }
    1079             : 
    1080           0 : static int ceph_snap_gmt_fntimes(vfs_handle_struct *handle,
    1081             :                                  files_struct *fsp,
    1082             :                                  struct smb_file_time *ft)
    1083             : {
    1084           0 :         time_t timestamp = 0;
    1085             :         int ret;
    1086             : 
    1087           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1088           0 :                                            fsp->fsp_name,
    1089             :                                            &timestamp,
    1090             :                                            NULL,
    1091             :                                            0);
    1092           0 :         if (ret < 0) {
    1093           0 :                 errno = -ret;
    1094           0 :                 return -1;
    1095             :         }
    1096           0 :         if (timestamp != 0) {
    1097           0 :                 errno = EROFS;
    1098           0 :                 return -1;
    1099             :         }
    1100           0 :         return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
    1101             : }
    1102             : 
    1103           0 : static int ceph_snap_gmt_readlinkat(vfs_handle_struct *handle,
    1104             :                                 const struct files_struct *dirfsp,
    1105             :                                 const struct smb_filename *csmb_fname,
    1106             :                                 char *buf,
    1107             :                                 size_t bufsiz)
    1108             : {
    1109           0 :         time_t timestamp = 0;
    1110             :         char conv[PATH_MAX + 1];
    1111             :         int ret;
    1112           0 :         struct smb_filename *full_fname = NULL;
    1113             :         int saved_errno;
    1114             : 
    1115             :         /*
    1116             :          * Now this function only looks at csmb_fname->twrp
    1117             :          * we don't need to copy out the path. Just use
    1118             :          * csmb_fname->base_name directly.
    1119             :          */
    1120           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1121             :                                         csmb_fname,
    1122             :                                         &timestamp, NULL, 0);
    1123           0 :         if (ret < 0) {
    1124           0 :                 errno = -ret;
    1125           0 :                 return -1;
    1126             :         }
    1127           0 :         if (timestamp == 0) {
    1128           0 :                 return SMB_VFS_NEXT_READLINKAT(handle,
    1129             :                                 dirfsp,
    1130             :                                 csmb_fname,
    1131             :                                 buf,
    1132             :                                 bufsiz);
    1133             :         }
    1134             : 
    1135           0 :         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
    1136             :                                                 dirfsp,
    1137             :                                                 csmb_fname);
    1138           0 :         if (full_fname == NULL) {
    1139           0 :                 return -1;
    1140             :         }
    1141             : 
    1142             :         /* Find the snapshot path from the full pathname. */
    1143           0 :         ret = ceph_snap_gmt_convert(handle,
    1144           0 :                                 full_fname->base_name,
    1145             :                                 timestamp,
    1146             :                                 conv,
    1147             :                                 sizeof(conv));
    1148           0 :         if (ret < 0) {
    1149           0 :                 TALLOC_FREE(full_fname);
    1150           0 :                 errno = -ret;
    1151           0 :                 return -1;
    1152             :         }
    1153           0 :         full_fname->base_name = conv;
    1154             : 
    1155           0 :         ret = SMB_VFS_NEXT_READLINKAT(handle,
    1156             :                                 handle->conn->cwd_fsp,
    1157             :                                 full_fname,
    1158             :                                 buf,
    1159             :                                 bufsiz);
    1160           0 :         saved_errno = errno;
    1161           0 :         TALLOC_FREE(full_fname);
    1162           0 :         errno = saved_errno;
    1163           0 :         return ret;
    1164             : }
    1165             : 
    1166           0 : static int ceph_snap_gmt_mknodat(vfs_handle_struct *handle,
    1167             :                         files_struct *dirfsp,
    1168             :                         const struct smb_filename *csmb_fname,
    1169             :                         mode_t mode,
    1170             :                         SMB_DEV_T dev)
    1171             : {
    1172           0 :         time_t timestamp = 0;
    1173             :         int ret;
    1174             : 
    1175           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1176             :                                         csmb_fname,
    1177             :                                         &timestamp, NULL, 0);
    1178           0 :         if (ret < 0) {
    1179           0 :                 errno = -ret;
    1180           0 :                 return -1;
    1181             :         }
    1182           0 :         if (timestamp != 0) {
    1183           0 :                 errno = EROFS;
    1184           0 :                 return -1;
    1185             :         }
    1186           0 :         return SMB_VFS_NEXT_MKNODAT(handle,
    1187             :                         dirfsp,
    1188             :                         csmb_fname,
    1189             :                         mode,
    1190             :                         dev);
    1191             : }
    1192             : 
    1193           0 : static struct smb_filename *ceph_snap_gmt_realpath(vfs_handle_struct *handle,
    1194             :                                 TALLOC_CTX *ctx,
    1195             :                                 const struct smb_filename *csmb_fname)
    1196             : {
    1197           0 :         time_t timestamp = 0;
    1198             :         char stripped[PATH_MAX + 1];
    1199             :         char conv[PATH_MAX + 1];
    1200             :         struct smb_filename *result_fname;
    1201             :         int ret;
    1202             :         struct smb_filename *new_fname;
    1203             :         int saved_errno;
    1204             : 
    1205           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1206             :                                         csmb_fname,
    1207             :                                         &timestamp, stripped, sizeof(stripped));
    1208           0 :         if (ret < 0) {
    1209           0 :                 errno = -ret;
    1210           0 :                 return NULL;
    1211             :         }
    1212           0 :         if (timestamp == 0) {
    1213           0 :                 return SMB_VFS_NEXT_REALPATH(handle, ctx, csmb_fname);
    1214             :         }
    1215           0 :         ret = ceph_snap_gmt_convert(handle, stripped,
    1216             :                                         timestamp, conv, sizeof(conv));
    1217           0 :         if (ret < 0) {
    1218           0 :                 errno = -ret;
    1219           0 :                 return NULL;
    1220             :         }
    1221           0 :         new_fname = cp_smb_filename(talloc_tos(), csmb_fname);
    1222           0 :         if (new_fname == NULL) {
    1223           0 :                 errno = ENOMEM;
    1224           0 :                 return NULL;
    1225             :         }
    1226           0 :         new_fname->base_name = conv;
    1227             : 
    1228           0 :         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, new_fname);
    1229           0 :         saved_errno = errno;
    1230           0 :         TALLOC_FREE(new_fname);
    1231           0 :         errno = saved_errno;
    1232           0 :         return result_fname;
    1233             : }
    1234             : 
    1235           0 : static int ceph_snap_gmt_mkdirat(vfs_handle_struct *handle,
    1236             :                                 struct files_struct *dirfsp,
    1237             :                                 const struct smb_filename *csmb_fname,
    1238             :                                 mode_t mode)
    1239             : {
    1240           0 :         time_t timestamp = 0;
    1241             :         int ret;
    1242             : 
    1243           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1244             :                                         csmb_fname,
    1245             :                                         &timestamp, NULL, 0);
    1246           0 :         if (ret < 0) {
    1247           0 :                 errno = -ret;
    1248           0 :                 return -1;
    1249             :         }
    1250           0 :         if (timestamp != 0) {
    1251           0 :                 errno = EROFS;
    1252           0 :                 return -1;
    1253             :         }
    1254           0 :         return SMB_VFS_NEXT_MKDIRAT(handle,
    1255             :                         dirfsp,
    1256             :                         csmb_fname,
    1257             :                         mode);
    1258             : }
    1259             : 
    1260           0 : static int ceph_snap_gmt_fchflags(vfs_handle_struct *handle,
    1261             :                                 struct files_struct *fsp,
    1262             :                                 unsigned int flags)
    1263             : {
    1264           0 :         time_t timestamp = 0;
    1265             :         int ret;
    1266             : 
    1267           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1268           0 :                                         fsp->fsp_name,
    1269             :                                         &timestamp, NULL, 0);
    1270           0 :         if (ret < 0) {
    1271           0 :                 errno = -ret;
    1272           0 :                 return -1;
    1273             :         }
    1274           0 :         if (timestamp != 0) {
    1275           0 :                 errno = EROFS;
    1276           0 :                 return -1;
    1277             :         }
    1278           0 :         return SMB_VFS_NEXT_FCHFLAGS(handle, fsp, flags);
    1279             : }
    1280             : 
    1281           0 : static int ceph_snap_gmt_fsetxattr(struct vfs_handle_struct *handle,
    1282             :                                 struct files_struct *fsp,
    1283             :                                 const char *aname, const void *value,
    1284             :                                 size_t size, int flags)
    1285             : {
    1286           0 :         const struct smb_filename *csmb_fname = NULL;
    1287           0 :         time_t timestamp = 0;
    1288             :         int ret;
    1289             : 
    1290           0 :         csmb_fname = fsp->fsp_name;
    1291           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1292             :                                         csmb_fname,
    1293             :                                         &timestamp, NULL, 0);
    1294           0 :         if (ret < 0) {
    1295           0 :                 errno = -ret;
    1296           0 :                 return -1;
    1297             :         }
    1298           0 :         if (timestamp != 0) {
    1299           0 :                 errno = EROFS;
    1300           0 :                 return -1;
    1301             :         }
    1302           0 :         return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
    1303             :                                 aname, value, size, flags);
    1304             : }
    1305             : 
    1306           0 : static int ceph_snap_gmt_get_real_filename(struct vfs_handle_struct *handle,
    1307             :                                          const struct smb_filename *path,
    1308             :                                          const char *name,
    1309             :                                          TALLOC_CTX *mem_ctx,
    1310             :                                          char **found_name)
    1311             : {
    1312           0 :         time_t timestamp = 0;
    1313             :         char stripped[PATH_MAX + 1];
    1314             :         char conv[PATH_MAX + 1];
    1315             :         struct smb_filename conv_fname;
    1316             :         int ret;
    1317             : 
    1318           0 :         ret = ceph_snap_gmt_strip_snapshot(handle, path,
    1319             :                                         &timestamp, stripped, sizeof(stripped));
    1320           0 :         if (ret < 0) {
    1321           0 :                 errno = -ret;
    1322           0 :                 return -1;
    1323             :         }
    1324           0 :         if (timestamp == 0) {
    1325           0 :                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
    1326             :                                                       mem_ctx, found_name);
    1327             :         }
    1328           0 :         ret = ceph_snap_gmt_convert_dir(handle, stripped,
    1329             :                                         timestamp, conv, sizeof(conv));
    1330           0 :         if (ret < 0) {
    1331           0 :                 errno = -ret;
    1332           0 :                 return -1;
    1333             :         }
    1334             : 
    1335           0 :         conv_fname = (struct smb_filename) {
    1336             :                 .base_name = conv,
    1337             :         };
    1338             : 
    1339           0 :         ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, &conv_fname, name,
    1340             :                                              mem_ctx, found_name);
    1341           0 :         return ret;
    1342             : }
    1343             : 
    1344           0 : static uint64_t ceph_snap_gmt_disk_free(vfs_handle_struct *handle,
    1345             :                                 const struct smb_filename *csmb_fname,
    1346             :                                 uint64_t *bsize,
    1347             :                                 uint64_t *dfree,
    1348             :                                 uint64_t *dsize)
    1349             : {
    1350           0 :         time_t timestamp = 0;
    1351             :         char stripped[PATH_MAX + 1];
    1352             :         char conv[PATH_MAX + 1];
    1353             :         int ret;
    1354             :         struct smb_filename *new_fname;
    1355             :         int saved_errno;
    1356             : 
    1357           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1358             :                                         csmb_fname,
    1359             :                                         &timestamp, stripped, sizeof(stripped));
    1360           0 :         if (ret < 0) {
    1361           0 :                 errno = -ret;
    1362           0 :                 return -1;
    1363             :         }
    1364           0 :         if (timestamp == 0) {
    1365           0 :                 return SMB_VFS_NEXT_DISK_FREE(handle, csmb_fname,
    1366             :                                               bsize, dfree, dsize);
    1367             :         }
    1368           0 :         ret = ceph_snap_gmt_convert(handle, stripped,
    1369             :                                         timestamp, conv, sizeof(conv));
    1370           0 :         if (ret < 0) {
    1371           0 :                 errno = -ret;
    1372           0 :                 return -1;
    1373             :         }
    1374           0 :         new_fname = cp_smb_filename(talloc_tos(), csmb_fname);
    1375           0 :         if (new_fname == NULL) {
    1376           0 :                 errno = ENOMEM;
    1377           0 :                 return -1;
    1378             :         }
    1379           0 :         new_fname->base_name = conv;
    1380             : 
    1381           0 :         ret = SMB_VFS_NEXT_DISK_FREE(handle, new_fname,
    1382             :                                 bsize, dfree, dsize);
    1383           0 :         saved_errno = errno;
    1384           0 :         TALLOC_FREE(new_fname);
    1385           0 :         errno = saved_errno;
    1386           0 :         return ret;
    1387             : }
    1388             : 
    1389           0 : static int ceph_snap_gmt_get_quota(vfs_handle_struct *handle,
    1390             :                         const struct smb_filename *csmb_fname,
    1391             :                         enum SMB_QUOTA_TYPE qtype,
    1392             :                         unid_t id,
    1393             :                         SMB_DISK_QUOTA *dq)
    1394             : {
    1395           0 :         time_t timestamp = 0;
    1396             :         char stripped[PATH_MAX + 1];
    1397             :         char conv[PATH_MAX + 1];
    1398             :         int ret;
    1399             :         struct smb_filename *new_fname;
    1400             :         int saved_errno;
    1401             : 
    1402           0 :         ret = ceph_snap_gmt_strip_snapshot(handle,
    1403             :                                         csmb_fname,
    1404             :                                         &timestamp, stripped, sizeof(stripped));
    1405           0 :         if (ret < 0) {
    1406           0 :                 errno = -ret;
    1407           0 :                 return -1;
    1408             :         }
    1409           0 :         if (timestamp == 0) {
    1410           0 :                 return SMB_VFS_NEXT_GET_QUOTA(handle, csmb_fname, qtype, id, dq);
    1411             :         }
    1412           0 :         ret = ceph_snap_gmt_convert(handle, stripped,
    1413             :                                         timestamp, conv, sizeof(conv));
    1414           0 :         if (ret < 0) {
    1415           0 :                 errno = -ret;
    1416           0 :                 return -1;
    1417             :         }
    1418           0 :         new_fname = cp_smb_filename(talloc_tos(), csmb_fname);
    1419           0 :         if (new_fname == NULL) {
    1420           0 :                 errno = ENOMEM;
    1421           0 :                 return -1;
    1422             :         }
    1423           0 :         new_fname->base_name = conv;
    1424             : 
    1425           0 :         ret = SMB_VFS_NEXT_GET_QUOTA(handle, new_fname, qtype, id, dq);
    1426           0 :         saved_errno = errno;
    1427           0 :         TALLOC_FREE(new_fname);
    1428           0 :         errno = saved_errno;
    1429           0 :         return ret;
    1430             : }
    1431             : 
    1432             : static struct vfs_fn_pointers ceph_snap_fns = {
    1433             :         .get_shadow_copy_data_fn = ceph_snap_get_shadow_copy_data,
    1434             :         .disk_free_fn = ceph_snap_gmt_disk_free,
    1435             :         .get_quota_fn = ceph_snap_gmt_get_quota,
    1436             :         .renameat_fn = ceph_snap_gmt_renameat,
    1437             :         .linkat_fn = ceph_snap_gmt_linkat,
    1438             :         .symlinkat_fn = ceph_snap_gmt_symlinkat,
    1439             :         .stat_fn = ceph_snap_gmt_stat,
    1440             :         .lstat_fn = ceph_snap_gmt_lstat,
    1441             :         .openat_fn = ceph_snap_gmt_openat,
    1442             :         .unlinkat_fn = ceph_snap_gmt_unlinkat,
    1443             :         .fchmod_fn = ceph_snap_gmt_fchmod,
    1444             :         .chdir_fn = ceph_snap_gmt_chdir,
    1445             :         .fntimes_fn = ceph_snap_gmt_fntimes,
    1446             :         .readlinkat_fn = ceph_snap_gmt_readlinkat,
    1447             :         .mknodat_fn = ceph_snap_gmt_mknodat,
    1448             :         .realpath_fn = ceph_snap_gmt_realpath,
    1449             :         .mkdirat_fn = ceph_snap_gmt_mkdirat,
    1450             :         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
    1451             :         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
    1452             :         .fsetxattr_fn = ceph_snap_gmt_fsetxattr,
    1453             :         .fchflags_fn = ceph_snap_gmt_fchflags,
    1454             :         .get_real_filename_fn = ceph_snap_gmt_get_real_filename,
    1455             : };
    1456             : 
    1457             : static_decl_vfs;
    1458          20 : NTSTATUS vfs_ceph_snapshots_init(TALLOC_CTX *ctx)
    1459             : {
    1460          20 :         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
    1461             :                                 "ceph_snapshots", &ceph_snap_fns);
    1462             : }

Generated by: LCOV version 1.13