coda_linux.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Inode operations for Coda filesystem
  4. * Original version: (C) 1996 P. Braam and M. Callahan
  5. * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
  6. *
  7. * Carnegie Mellon encourages users to contribute improvements to
  8. * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/time.h>
  13. #include <linux/fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/errno.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/string.h>
  18. #include <linux/coda.h>
  19. #include "coda_psdev.h"
  20. #include "coda_linux.h"
  21. /* initialize the debugging variables */
  22. int coda_fake_statfs;
  23. /* print a fid */
  24. char * coda_f2s(struct CodaFid *f)
  25. {
  26. static char s[60];
  27. sprintf(s, "(%08x.%08x.%08x.%08x)", f->opaque[0], f->opaque[1], f->opaque[2], f->opaque[3]);
  28. return s;
  29. }
  30. /* recognize special .CONTROL name */
  31. int coda_iscontrol(const char *name, size_t length)
  32. {
  33. return ((CODA_CONTROLLEN == length) &&
  34. (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0));
  35. }
  36. unsigned short coda_flags_to_cflags(unsigned short flags)
  37. {
  38. unsigned short coda_flags = 0;
  39. if ((flags & O_ACCMODE) == O_RDONLY)
  40. coda_flags |= C_O_READ;
  41. if ((flags & O_ACCMODE) == O_RDWR)
  42. coda_flags |= C_O_READ | C_O_WRITE;
  43. if ((flags & O_ACCMODE) == O_WRONLY)
  44. coda_flags |= C_O_WRITE;
  45. if (flags & O_TRUNC)
  46. coda_flags |= C_O_TRUNC;
  47. if (flags & O_CREAT)
  48. coda_flags |= C_O_CREAT;
  49. if (flags & O_EXCL)
  50. coda_flags |= C_O_EXCL;
  51. return coda_flags;
  52. }
  53. static struct timespec64 coda_to_timespec64(struct coda_timespec ts)
  54. {
  55. struct timespec64 ts64 = {
  56. .tv_sec = ts.tv_sec,
  57. .tv_nsec = ts.tv_nsec,
  58. };
  59. return ts64;
  60. }
  61. static struct coda_timespec timespec64_to_coda(struct timespec64 ts64)
  62. {
  63. struct coda_timespec ts = {
  64. .tv_sec = ts64.tv_sec,
  65. .tv_nsec = ts64.tv_nsec,
  66. };
  67. return ts;
  68. }
  69. /* utility functions below */
  70. void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr)
  71. {
  72. int inode_type;
  73. /* inode's i_flags, i_ino are set by iget
  74. XXX: is this all we need ??
  75. */
  76. switch (attr->va_type) {
  77. case C_VNON:
  78. inode_type = 0;
  79. break;
  80. case C_VREG:
  81. inode_type = S_IFREG;
  82. break;
  83. case C_VDIR:
  84. inode_type = S_IFDIR;
  85. break;
  86. case C_VLNK:
  87. inode_type = S_IFLNK;
  88. break;
  89. default:
  90. inode_type = 0;
  91. }
  92. inode->i_mode |= inode_type;
  93. if (attr->va_mode != (u_short) -1)
  94. inode->i_mode = attr->va_mode | inode_type;
  95. if (attr->va_uid != -1)
  96. inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid);
  97. if (attr->va_gid != -1)
  98. inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid);
  99. if (attr->va_nlink != -1)
  100. set_nlink(inode, attr->va_nlink);
  101. if (attr->va_size != -1)
  102. inode->i_size = attr->va_size;
  103. if (attr->va_size != -1)
  104. inode->i_blocks = (attr->va_size + 511) >> 9;
  105. if (attr->va_atime.tv_sec != -1)
  106. inode->i_atime = coda_to_timespec64(attr->va_atime);
  107. if (attr->va_mtime.tv_sec != -1)
  108. inode->i_mtime = coda_to_timespec64(attr->va_mtime);
  109. if (attr->va_ctime.tv_sec != -1)
  110. inode->i_ctime = coda_to_timespec64(attr->va_ctime);
  111. }
  112. /*
  113. * BSD sets attributes that need not be modified to -1.
  114. * Linux uses the valid field to indicate what should be
  115. * looked at. The BSD type field needs to be deduced from linux
  116. * mode.
  117. * So we have to do some translations here.
  118. */
  119. void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr)
  120. {
  121. unsigned int valid;
  122. /* clean out */
  123. vattr->va_mode = -1;
  124. vattr->va_uid = (vuid_t) -1;
  125. vattr->va_gid = (vgid_t) -1;
  126. vattr->va_size = (off_t) -1;
  127. vattr->va_atime.tv_sec = (int64_t) -1;
  128. vattr->va_atime.tv_nsec = (long) -1;
  129. vattr->va_mtime.tv_sec = (int64_t) -1;
  130. vattr->va_mtime.tv_nsec = (long) -1;
  131. vattr->va_ctime.tv_sec = (int64_t) -1;
  132. vattr->va_ctime.tv_nsec = (long) -1;
  133. vattr->va_type = C_VNON;
  134. vattr->va_fileid = -1;
  135. vattr->va_gen = -1;
  136. vattr->va_bytes = -1;
  137. vattr->va_nlink = -1;
  138. vattr->va_blocksize = -1;
  139. vattr->va_rdev = -1;
  140. vattr->va_flags = 0;
  141. /* determine the type */
  142. #if 0
  143. mode = iattr->ia_mode;
  144. if ( S_ISDIR(mode) ) {
  145. vattr->va_type = C_VDIR;
  146. } else if ( S_ISREG(mode) ) {
  147. vattr->va_type = C_VREG;
  148. } else if ( S_ISLNK(mode) ) {
  149. vattr->va_type = C_VLNK;
  150. } else {
  151. /* don't do others */
  152. vattr->va_type = C_VNON;
  153. }
  154. #endif
  155. /* set those vattrs that need change */
  156. valid = iattr->ia_valid;
  157. if ( valid & ATTR_MODE ) {
  158. vattr->va_mode = iattr->ia_mode;
  159. }
  160. if ( valid & ATTR_UID ) {
  161. vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid);
  162. }
  163. if ( valid & ATTR_GID ) {
  164. vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid);
  165. }
  166. if ( valid & ATTR_SIZE ) {
  167. vattr->va_size = iattr->ia_size;
  168. }
  169. if ( valid & ATTR_ATIME ) {
  170. vattr->va_atime = timespec64_to_coda(iattr->ia_atime);
  171. }
  172. if ( valid & ATTR_MTIME ) {
  173. vattr->va_mtime = timespec64_to_coda(iattr->ia_mtime);
  174. }
  175. if ( valid & ATTR_CTIME ) {
  176. vattr->va_ctime = timespec64_to_coda(iattr->ia_ctime);
  177. }
  178. }