nfs-ganesha 1.4

cache_inode_truncate.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  * ---------------------------------------
00024  */
00025 
00037 #ifdef HAVE_CONFIG_H
00038 #include "config.h"
00039 #endif
00040 
00041 #ifdef _SOLARIS
00042 #include "solaris_port.h"
00043 #endif                          /* _SOLARIS */
00044 
00045 #include "fsal.h"
00046 #include "log.h"
00047 #include "HashData.h"
00048 #include "HashTable.h"
00049 #include "cache_inode.h"
00050 
00051 #include <unistd.h>
00052 #include <sys/types.h>
00053 #include <sys/param.h>
00054 #include <time.h>
00055 #include <pthread.h>
00056 #include <assert.h>
00057 
00072 cache_inode_status_t
00073 cache_inode_truncate_impl(cache_entry_t *entry,
00074                           fsal_size_t length,
00075                           fsal_attrib_list_t *attr,
00076                           fsal_op_context_t *context,
00077                           cache_inode_status_t *status)
00078 {
00079   fsal_status_t fsal_status;
00080   fsal_file_t *fd;
00081 
00082   /* Set the return default to CACHE_INODE_SUCCESS */
00083   *status = CACHE_INODE_SUCCESS;
00084 
00085   /* Only regular files can be truncated */
00086   if(entry->type != REGULAR_FILE)
00087     {
00088       *status = CACHE_INODE_BAD_TYPE;
00089       return *status;
00090     }
00091 
00092   /* Call FSAL to actually truncate */
00093   entry->attributes.asked_attributes = cache_inode_params.attrmask;
00094   if (entry->object.file.open_fd.openflags == FSAL_O_CLOSED)
00095     fd = NULL;
00096   else
00097     fd = &(entry->object.file.open_fd.fd);
00098   fsal_status = FSAL_truncate(&entry->handle, context, length,
00099                               fd, /* used by FSAL_GPFS */
00100                               &entry->attributes);
00101 
00102   if(FSAL_IS_ERROR(fsal_status))
00103     {
00104       *status = cache_inode_error_convert(fsal_status);
00105       if (fsal_status.major == ERR_FSAL_STALE) {
00106         cache_inode_kill_entry(entry);
00107       }
00108       return *status;
00109     }
00110 
00111   /* Returns the attributes */
00112   *attr = entry->attributes;
00113 
00114   return *status;
00115 }                               /* cache_inode_truncate_sw */
00116 
00132 cache_inode_status_t
00133 cache_inode_truncate(cache_entry_t *entry,
00134                      fsal_size_t length,
00135                      fsal_attrib_list_t *attr,
00136                      fsal_op_context_t *context,
00137                      cache_inode_status_t *status)
00138 {
00139   cache_inode_status_t ret;
00140   pthread_rwlock_wrlock(&entry->attr_lock);
00141   pthread_rwlock_wrlock(&entry->content_lock);
00142   ret = cache_inode_truncate_impl(entry,
00143                                   length, attr, context, status);
00144   pthread_rwlock_unlock(&entry->attr_lock);
00145   pthread_rwlock_unlock(&entry->content_lock);
00146   return ret;
00147 } /* cache_inode_truncate */