inode.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2007
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/pagemap.h>
  24. #include <asm/div64.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. #include "cifs_fs_sb.h"
  31. int cifs_get_inode_info_unix(struct inode **pinode,
  32. const unsigned char *search_path, struct super_block *sb, int xid)
  33. {
  34. int rc = 0;
  35. FILE_UNIX_BASIC_INFO findData;
  36. struct cifsTconInfo *pTcon;
  37. struct inode *inode;
  38. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  39. char *tmp_path;
  40. pTcon = cifs_sb->tcon;
  41. cFYI(1, ("Getting info on %s", search_path));
  42. /* could have done a find first instead but this returns more info */
  43. rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
  44. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  45. CIFS_MOUNT_MAP_SPECIAL_CHR);
  46. /* dump_mem("\nUnixQPathInfo return data", &findData,
  47. sizeof(findData)); */
  48. if (rc) {
  49. if (rc == -EREMOTE) {
  50. tmp_path =
  51. kmalloc(strnlen(pTcon->treeName,
  52. MAX_TREE_SIZE + 1) +
  53. strnlen(search_path, MAX_PATHCONF) + 1,
  54. GFP_KERNEL);
  55. if (tmp_path == NULL) {
  56. return -ENOMEM;
  57. }
  58. /* have to skip first of the double backslash of
  59. UNC name */
  60. strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
  61. strncat(tmp_path, search_path, MAX_PATHCONF);
  62. rc = connect_to_dfs_path(xid, pTcon->ses,
  63. /* treename + */ tmp_path,
  64. cifs_sb->local_nls,
  65. cifs_sb->mnt_cifs_flags &
  66. CIFS_MOUNT_MAP_SPECIAL_CHR);
  67. kfree(tmp_path);
  68. /* BB fix up inode etc. */
  69. } else if (rc) {
  70. return rc;
  71. }
  72. } else {
  73. struct cifsInodeInfo *cifsInfo;
  74. __u32 type = le32_to_cpu(findData.Type);
  75. __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
  76. __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
  77. /* get new inode */
  78. if (*pinode == NULL) {
  79. *pinode = new_inode(sb);
  80. if (*pinode == NULL)
  81. return -ENOMEM;
  82. /* Is an i_ino of zero legal? */
  83. /* Are there sanity checks we can use to ensure that
  84. the server is really filling in that field? */
  85. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  86. (*pinode)->i_ino =
  87. (unsigned long)findData.UniqueId;
  88. } /* note ino incremented to unique num in new_inode */
  89. if (sb->s_flags & MS_NOATIME)
  90. (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
  91. insert_inode_hash(*pinode);
  92. }
  93. inode = *pinode;
  94. cifsInfo = CIFS_I(inode);
  95. cFYI(1, ("Old time %ld", cifsInfo->time));
  96. cifsInfo->time = jiffies;
  97. cFYI(1, ("New time %ld", cifsInfo->time));
  98. /* this is ok to set on every inode revalidate */
  99. atomic_set(&cifsInfo->inUse, 1);
  100. inode->i_atime =
  101. cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
  102. inode->i_mtime =
  103. cifs_NTtimeToUnix(le64_to_cpu
  104. (findData.LastModificationTime));
  105. inode->i_ctime =
  106. cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
  107. inode->i_mode = le64_to_cpu(findData.Permissions);
  108. /* since we set the inode type below we need to mask off
  109. to avoid strange results if bits set above */
  110. inode->i_mode &= ~S_IFMT;
  111. if (type == UNIX_FILE) {
  112. inode->i_mode |= S_IFREG;
  113. } else if (type == UNIX_SYMLINK) {
  114. inode->i_mode |= S_IFLNK;
  115. } else if (type == UNIX_DIR) {
  116. inode->i_mode |= S_IFDIR;
  117. } else if (type == UNIX_CHARDEV) {
  118. inode->i_mode |= S_IFCHR;
  119. inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
  120. le64_to_cpu(findData.DevMinor) & MINORMASK);
  121. } else if (type == UNIX_BLOCKDEV) {
  122. inode->i_mode |= S_IFBLK;
  123. inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
  124. le64_to_cpu(findData.DevMinor) & MINORMASK);
  125. } else if (type == UNIX_FIFO) {
  126. inode->i_mode |= S_IFIFO;
  127. } else if (type == UNIX_SOCKET) {
  128. inode->i_mode |= S_IFSOCK;
  129. } else {
  130. /* safest to call it a file if we do not know */
  131. inode->i_mode |= S_IFREG;
  132. cFYI(1, ("unknown type %d", type));
  133. }
  134. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
  135. inode->i_uid = cifs_sb->mnt_uid;
  136. else
  137. inode->i_uid = le64_to_cpu(findData.Uid);
  138. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
  139. inode->i_gid = cifs_sb->mnt_gid;
  140. else
  141. inode->i_gid = le64_to_cpu(findData.Gid);
  142. inode->i_nlink = le64_to_cpu(findData.Nlinks);
  143. spin_lock(&inode->i_lock);
  144. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  145. /* can not safely change the file size here if the
  146. client is writing to it due to potential races */
  147. i_size_write(inode, end_of_file);
  148. /* blksize needs to be multiple of two. So safer to default to
  149. blksize and blkbits set in superblock so 2**blkbits and blksize
  150. will match rather than setting to:
  151. (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
  152. /* This seems incredibly stupid but it turns out that i_blocks
  153. is not related to (i_size / i_blksize), instead 512 byte size
  154. is required for calculating num blocks */
  155. /* 512 bytes (2**9) is the fake blocksize that must be used */
  156. /* for this calculation */
  157. inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  158. }
  159. spin_unlock(&inode->i_lock);
  160. if (num_of_bytes < end_of_file)
  161. cFYI(1, ("allocation size less than end of file"));
  162. cFYI(1, ("Size %ld and blocks %llu",
  163. (unsigned long) inode->i_size,
  164. (unsigned long long)inode->i_blocks));
  165. if (S_ISREG(inode->i_mode)) {
  166. cFYI(1, ("File inode"));
  167. inode->i_op = &cifs_file_inode_ops;
  168. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  169. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  170. inode->i_fop =
  171. &cifs_file_direct_nobrl_ops;
  172. else
  173. inode->i_fop = &cifs_file_direct_ops;
  174. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  175. inode->i_fop = &cifs_file_nobrl_ops;
  176. else /* not direct, send byte range locks */
  177. inode->i_fop = &cifs_file_ops;
  178. /* check if server can support readpages */
  179. if (pTcon->ses->server->maxBuf <
  180. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  181. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  182. else
  183. inode->i_data.a_ops = &cifs_addr_ops;
  184. } else if (S_ISDIR(inode->i_mode)) {
  185. cFYI(1, ("Directory inode"));
  186. inode->i_op = &cifs_dir_inode_ops;
  187. inode->i_fop = &cifs_dir_ops;
  188. } else if (S_ISLNK(inode->i_mode)) {
  189. cFYI(1, ("Symbolic Link inode"));
  190. inode->i_op = &cifs_symlink_inode_ops;
  191. /* tmp_inode->i_fop = */ /* do not need to set to anything */
  192. } else {
  193. cFYI(1, ("Init special inode"));
  194. init_special_inode(inode, inode->i_mode,
  195. inode->i_rdev);
  196. }
  197. }
  198. return rc;
  199. }
  200. static int decode_sfu_inode(struct inode *inode, __u64 size,
  201. const unsigned char *path,
  202. struct cifs_sb_info *cifs_sb, int xid)
  203. {
  204. int rc;
  205. int oplock = FALSE;
  206. __u16 netfid;
  207. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  208. char buf[24];
  209. unsigned int bytes_read;
  210. char *pbuf;
  211. pbuf = buf;
  212. if (size == 0) {
  213. inode->i_mode |= S_IFIFO;
  214. return 0;
  215. } else if (size < 8) {
  216. return -EINVAL; /* EOPNOTSUPP? */
  217. }
  218. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  219. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  220. cifs_sb->local_nls,
  221. cifs_sb->mnt_cifs_flags &
  222. CIFS_MOUNT_MAP_SPECIAL_CHR);
  223. if (rc == 0) {
  224. int buf_type = CIFS_NO_BUFFER;
  225. /* Read header */
  226. rc = CIFSSMBRead(xid, pTcon,
  227. netfid,
  228. 24 /* length */, 0 /* offset */,
  229. &bytes_read, &pbuf, &buf_type);
  230. if ((rc == 0) && (bytes_read >= 8)) {
  231. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  232. cFYI(1, ("Block device"));
  233. inode->i_mode |= S_IFBLK;
  234. if (bytes_read == 24) {
  235. /* we have enough to decode dev num */
  236. __u64 mjr; /* major */
  237. __u64 mnr; /* minor */
  238. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  239. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  240. inode->i_rdev = MKDEV(mjr, mnr);
  241. }
  242. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  243. cFYI(1, ("Char device"));
  244. inode->i_mode |= S_IFCHR;
  245. if (bytes_read == 24) {
  246. /* we have enough to decode dev num */
  247. __u64 mjr; /* major */
  248. __u64 mnr; /* minor */
  249. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  250. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  251. inode->i_rdev = MKDEV(mjr, mnr);
  252. }
  253. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  254. cFYI(1, ("Symlink"));
  255. inode->i_mode |= S_IFLNK;
  256. } else {
  257. inode->i_mode |= S_IFREG; /* file? */
  258. rc = -EOPNOTSUPP;
  259. }
  260. } else {
  261. inode->i_mode |= S_IFREG; /* then it is a file */
  262. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  263. }
  264. CIFSSMBClose(xid, pTcon, netfid);
  265. }
  266. return rc;
  267. }
  268. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  269. static int get_sfu_uid_mode(struct inode *inode,
  270. const unsigned char *path,
  271. struct cifs_sb_info *cifs_sb, int xid)
  272. {
  273. #ifdef CONFIG_CIFS_XATTR
  274. ssize_t rc;
  275. char ea_value[4];
  276. __u32 mode;
  277. rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
  278. ea_value, 4 /* size of buf */, cifs_sb->local_nls,
  279. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  280. if (rc < 0)
  281. return (int)rc;
  282. else if (rc > 3) {
  283. mode = le32_to_cpu(*((__le32 *)ea_value));
  284. inode->i_mode &= ~SFBITS_MASK;
  285. cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
  286. inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
  287. cFYI(1, ("special mode bits 0%o", mode));
  288. return 0;
  289. } else {
  290. return 0;
  291. }
  292. #else
  293. return -EOPNOTSUPP;
  294. #endif
  295. }
  296. int cifs_get_inode_info(struct inode **pinode,
  297. const unsigned char *search_path, FILE_ALL_INFO *pfindData,
  298. struct super_block *sb, int xid)
  299. {
  300. int rc = 0;
  301. struct cifsTconInfo *pTcon;
  302. struct inode *inode;
  303. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  304. char *tmp_path;
  305. char *buf = NULL;
  306. int adjustTZ = FALSE;
  307. pTcon = cifs_sb->tcon;
  308. cFYI(1, ("Getting info on %s", search_path));
  309. if ((pfindData == NULL) && (*pinode != NULL)) {
  310. if (CIFS_I(*pinode)->clientCanCacheRead) {
  311. cFYI(1, ("No need to revalidate cached inode sizes"));
  312. return rc;
  313. }
  314. }
  315. /* if file info not passed in then get it from server */
  316. if (pfindData == NULL) {
  317. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  318. if (buf == NULL)
  319. return -ENOMEM;
  320. pfindData = (FILE_ALL_INFO *)buf;
  321. /* could do find first instead but this returns more info */
  322. rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
  323. 0 /* not legacy */,
  324. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  325. CIFS_MOUNT_MAP_SPECIAL_CHR);
  326. /* BB optimize code so we do not make the above call
  327. when server claims no NT SMB support and the above call
  328. failed at least once - set flag in tcon or mount */
  329. if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
  330. rc = SMBQueryInformation(xid, pTcon, search_path,
  331. pfindData, cifs_sb->local_nls,
  332. cifs_sb->mnt_cifs_flags &
  333. CIFS_MOUNT_MAP_SPECIAL_CHR);
  334. adjustTZ = TRUE;
  335. }
  336. }
  337. /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
  338. if (rc) {
  339. if (rc == -EREMOTE) {
  340. tmp_path =
  341. kmalloc(strnlen
  342. (pTcon->treeName,
  343. MAX_TREE_SIZE + 1) +
  344. strnlen(search_path, MAX_PATHCONF) + 1,
  345. GFP_KERNEL);
  346. if (tmp_path == NULL) {
  347. kfree(buf);
  348. return -ENOMEM;
  349. }
  350. strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
  351. strncat(tmp_path, search_path, MAX_PATHCONF);
  352. rc = connect_to_dfs_path(xid, pTcon->ses,
  353. /* treename + */ tmp_path,
  354. cifs_sb->local_nls,
  355. cifs_sb->mnt_cifs_flags &
  356. CIFS_MOUNT_MAP_SPECIAL_CHR);
  357. kfree(tmp_path);
  358. /* BB fix up inode etc. */
  359. } else if (rc) {
  360. kfree(buf);
  361. return rc;
  362. }
  363. } else {
  364. struct cifsInodeInfo *cifsInfo;
  365. __u32 attr = le32_to_cpu(pfindData->Attributes);
  366. /* get new inode */
  367. if (*pinode == NULL) {
  368. *pinode = new_inode(sb);
  369. if (*pinode == NULL) {
  370. kfree(buf);
  371. return -ENOMEM;
  372. }
  373. /* Is an i_ino of zero legal? Can we use that to check
  374. if the server supports returning inode numbers? Are
  375. there other sanity checks we can use to ensure that
  376. the server is really filling in that field? */
  377. /* We can not use the IndexNumber field by default from
  378. Windows or Samba (in ALL_INFO buf) but we can request
  379. it explicitly. It may not be unique presumably if
  380. the server has multiple devices mounted under one
  381. share */
  382. /* There may be higher info levels that work but are
  383. there Windows server or network appliances for which
  384. IndexNumber field is not guaranteed unique? */
  385. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  386. int rc1 = 0;
  387. __u64 inode_num;
  388. rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
  389. search_path, &inode_num,
  390. cifs_sb->local_nls,
  391. cifs_sb->mnt_cifs_flags &
  392. CIFS_MOUNT_MAP_SPECIAL_CHR);
  393. if (rc1) {
  394. cFYI(1, ("GetSrvInodeNum rc %d", rc1));
  395. /* BB EOPNOSUPP disable SERVER_INUM? */
  396. } else /* do we need cast or hash to ino? */
  397. (*pinode)->i_ino = inode_num;
  398. } /* else ino incremented to unique num in new_inode*/
  399. if (sb->s_flags & MS_NOATIME)
  400. (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
  401. insert_inode_hash(*pinode);
  402. }
  403. inode = *pinode;
  404. cifsInfo = CIFS_I(inode);
  405. cifsInfo->cifsAttrs = attr;
  406. cFYI(1, ("Old time %ld", cifsInfo->time));
  407. cifsInfo->time = jiffies;
  408. cFYI(1, ("New time %ld", cifsInfo->time));
  409. /* blksize needs to be multiple of two. So safer to default to
  410. blksize and blkbits set in superblock so 2**blkbits and blksize
  411. will match rather than setting to:
  412. (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
  413. /* Linux can not store file creation time so ignore it */
  414. if (pfindData->LastAccessTime)
  415. inode->i_atime = cifs_NTtimeToUnix
  416. (le64_to_cpu(pfindData->LastAccessTime));
  417. else /* do not need to use current_fs_time - time not stored */
  418. inode->i_atime = CURRENT_TIME;
  419. inode->i_mtime =
  420. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  421. inode->i_ctime =
  422. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  423. cFYI(0, ("Attributes came in as 0x%x", attr));
  424. if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
  425. inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
  426. inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
  427. }
  428. /* set default mode. will override for dirs below */
  429. if (atomic_read(&cifsInfo->inUse) == 0)
  430. /* new inode, can safely set these fields */
  431. inode->i_mode = cifs_sb->mnt_file_mode;
  432. else /* since we set the inode type below we need to mask off
  433. to avoid strange results if type changes and both
  434. get orred in */
  435. inode->i_mode &= ~S_IFMT;
  436. /* if (attr & ATTR_REPARSE) */
  437. /* We no longer handle these as symlinks because we could not
  438. follow them due to the absolute path with drive letter */
  439. if (attr & ATTR_DIRECTORY) {
  440. /* override default perms since we do not do byte range locking
  441. on dirs */
  442. inode->i_mode = cifs_sb->mnt_dir_mode;
  443. inode->i_mode |= S_IFDIR;
  444. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  445. (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
  446. /* No need to le64 convert size of zero */
  447. (pfindData->EndOfFile == 0)) {
  448. inode->i_mode = cifs_sb->mnt_file_mode;
  449. inode->i_mode |= S_IFIFO;
  450. /* BB Finish for SFU style symlinks and devices */
  451. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  452. (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
  453. if (decode_sfu_inode(inode,
  454. le64_to_cpu(pfindData->EndOfFile),
  455. search_path,
  456. cifs_sb, xid)) {
  457. cFYI(1, ("Unrecognized sfu inode type"));
  458. }
  459. cFYI(1, ("sfu mode 0%o", inode->i_mode));
  460. } else {
  461. inode->i_mode |= S_IFREG;
  462. /* treat the dos attribute of read-only as read-only
  463. mode e.g. 555 */
  464. if (cifsInfo->cifsAttrs & ATTR_READONLY)
  465. inode->i_mode &= ~(S_IWUGO);
  466. else if ((inode->i_mode & S_IWUGO) == 0)
  467. /* the ATTR_READONLY flag may have been */
  468. /* changed on server -- set any w bits */
  469. /* allowed by mnt_file_mode */
  470. inode->i_mode |= (S_IWUGO &
  471. cifs_sb->mnt_file_mode);
  472. /* BB add code here -
  473. validate if device or weird share or device type? */
  474. }
  475. spin_lock(&inode->i_lock);
  476. if (is_size_safe_to_change(cifsInfo, le64_to_cpu(pfindData->EndOfFile))) {
  477. /* can not safely shrink the file size here if the
  478. client is writing to it due to potential races */
  479. i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
  480. /* 512 bytes (2**9) is the fake blocksize that must be
  481. used for this calculation */
  482. inode->i_blocks = (512 - 1 + le64_to_cpu(
  483. pfindData->AllocationSize)) >> 9;
  484. }
  485. spin_unlock(&inode->i_lock);
  486. inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
  487. /* BB fill in uid and gid here? with help from winbind?
  488. or retrieve from NTFS stream extended attribute */
  489. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  490. /* fill in uid, gid, mode from server ACL */
  491. /* BB FIXME this should also take into account the
  492. * default uid specified on mount if present */
  493. get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
  494. } else if (atomic_read(&cifsInfo->inUse) == 0) {
  495. inode->i_uid = cifs_sb->mnt_uid;
  496. inode->i_gid = cifs_sb->mnt_gid;
  497. /* set so we do not keep refreshing these fields with
  498. bad data after user has changed them in memory */
  499. atomic_set(&cifsInfo->inUse, 1);
  500. }
  501. if (S_ISREG(inode->i_mode)) {
  502. cFYI(1, ("File inode"));
  503. inode->i_op = &cifs_file_inode_ops;
  504. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  505. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  506. inode->i_fop =
  507. &cifs_file_direct_nobrl_ops;
  508. else
  509. inode->i_fop = &cifs_file_direct_ops;
  510. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  511. inode->i_fop = &cifs_file_nobrl_ops;
  512. else /* not direct, send byte range locks */
  513. inode->i_fop = &cifs_file_ops;
  514. if (pTcon->ses->server->maxBuf <
  515. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  516. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  517. else
  518. inode->i_data.a_ops = &cifs_addr_ops;
  519. } else if (S_ISDIR(inode->i_mode)) {
  520. cFYI(1, ("Directory inode"));
  521. inode->i_op = &cifs_dir_inode_ops;
  522. inode->i_fop = &cifs_dir_ops;
  523. } else if (S_ISLNK(inode->i_mode)) {
  524. cFYI(1, ("Symbolic Link inode"));
  525. inode->i_op = &cifs_symlink_inode_ops;
  526. } else {
  527. init_special_inode(inode, inode->i_mode,
  528. inode->i_rdev);
  529. }
  530. }
  531. kfree(buf);
  532. return rc;
  533. }
  534. /* gets root inode */
  535. void cifs_read_inode(struct inode *inode)
  536. {
  537. int xid;
  538. struct cifs_sb_info *cifs_sb;
  539. cifs_sb = CIFS_SB(inode->i_sb);
  540. xid = GetXid();
  541. if (cifs_sb->tcon->unix_ext)
  542. cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
  543. else
  544. cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid);
  545. /* can not call macro FreeXid here since in a void func */
  546. _FreeXid(xid);
  547. }
  548. int cifs_unlink(struct inode *inode, struct dentry *direntry)
  549. {
  550. int rc = 0;
  551. int xid;
  552. struct cifs_sb_info *cifs_sb;
  553. struct cifsTconInfo *pTcon;
  554. char *full_path = NULL;
  555. struct cifsInodeInfo *cifsInode;
  556. FILE_BASIC_INFO *pinfo_buf;
  557. cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
  558. xid = GetXid();
  559. if (inode)
  560. cifs_sb = CIFS_SB(inode->i_sb);
  561. else
  562. cifs_sb = CIFS_SB(direntry->d_sb);
  563. pTcon = cifs_sb->tcon;
  564. /* Unlink can be called from rename so we can not grab the sem here
  565. since we deadlock otherwise */
  566. /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
  567. full_path = build_path_from_dentry(direntry);
  568. /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
  569. if (full_path == NULL) {
  570. FreeXid(xid);
  571. return -ENOMEM;
  572. }
  573. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  574. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  575. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  576. rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
  577. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  578. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  579. cFYI(1, ("posix del rc %d", rc));
  580. if ((rc == 0) || (rc == -ENOENT))
  581. goto psx_del_no_retry;
  582. }
  583. rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
  584. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  585. psx_del_no_retry:
  586. if (!rc) {
  587. if (direntry->d_inode)
  588. drop_nlink(direntry->d_inode);
  589. } else if (rc == -ENOENT) {
  590. d_drop(direntry);
  591. } else if (rc == -ETXTBSY) {
  592. int oplock = FALSE;
  593. __u16 netfid;
  594. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
  595. CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
  596. &netfid, &oplock, NULL, cifs_sb->local_nls,
  597. cifs_sb->mnt_cifs_flags &
  598. CIFS_MOUNT_MAP_SPECIAL_CHR);
  599. if (rc == 0) {
  600. CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
  601. cifs_sb->local_nls,
  602. cifs_sb->mnt_cifs_flags &
  603. CIFS_MOUNT_MAP_SPECIAL_CHR);
  604. CIFSSMBClose(xid, pTcon, netfid);
  605. if (direntry->d_inode)
  606. drop_nlink(direntry->d_inode);
  607. }
  608. } else if (rc == -EACCES) {
  609. /* try only if r/o attribute set in local lookup data? */
  610. pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
  611. if (pinfo_buf) {
  612. /* ATTRS set to normal clears r/o bit */
  613. pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
  614. if (!(pTcon->ses->flags & CIFS_SES_NT4))
  615. rc = CIFSSMBSetTimes(xid, pTcon, full_path,
  616. pinfo_buf,
  617. cifs_sb->local_nls,
  618. cifs_sb->mnt_cifs_flags &
  619. CIFS_MOUNT_MAP_SPECIAL_CHR);
  620. else
  621. rc = -EOPNOTSUPP;
  622. if (rc == -EOPNOTSUPP) {
  623. int oplock = FALSE;
  624. __u16 netfid;
  625. /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
  626. full_path,
  627. (__u16)ATTR_NORMAL,
  628. cifs_sb->local_nls);
  629. For some strange reason it seems that NT4 eats the
  630. old setattr call without actually setting the
  631. attributes so on to the third attempted workaround
  632. */
  633. /* BB could scan to see if we already have it open
  634. and pass in pid of opener to function */
  635. rc = CIFSSMBOpen(xid, pTcon, full_path,
  636. FILE_OPEN, SYNCHRONIZE |
  637. FILE_WRITE_ATTRIBUTES, 0,
  638. &netfid, &oplock, NULL,
  639. cifs_sb->local_nls,
  640. cifs_sb->mnt_cifs_flags &
  641. CIFS_MOUNT_MAP_SPECIAL_CHR);
  642. if (rc == 0) {
  643. rc = CIFSSMBSetFileTimes(xid, pTcon,
  644. pinfo_buf,
  645. netfid);
  646. CIFSSMBClose(xid, pTcon, netfid);
  647. }
  648. }
  649. kfree(pinfo_buf);
  650. }
  651. if (rc == 0) {
  652. rc = CIFSSMBDelFile(xid, pTcon, full_path,
  653. cifs_sb->local_nls,
  654. cifs_sb->mnt_cifs_flags &
  655. CIFS_MOUNT_MAP_SPECIAL_CHR);
  656. if (!rc) {
  657. if (direntry->d_inode)
  658. drop_nlink(direntry->d_inode);
  659. } else if (rc == -ETXTBSY) {
  660. int oplock = FALSE;
  661. __u16 netfid;
  662. rc = CIFSSMBOpen(xid, pTcon, full_path,
  663. FILE_OPEN, DELETE,
  664. CREATE_NOT_DIR |
  665. CREATE_DELETE_ON_CLOSE,
  666. &netfid, &oplock, NULL,
  667. cifs_sb->local_nls,
  668. cifs_sb->mnt_cifs_flags &
  669. CIFS_MOUNT_MAP_SPECIAL_CHR);
  670. if (rc == 0) {
  671. CIFSSMBRenameOpenFile(xid, pTcon,
  672. netfid, NULL,
  673. cifs_sb->local_nls,
  674. cifs_sb->mnt_cifs_flags &
  675. CIFS_MOUNT_MAP_SPECIAL_CHR);
  676. CIFSSMBClose(xid, pTcon, netfid);
  677. if (direntry->d_inode)
  678. drop_nlink(direntry->d_inode);
  679. }
  680. /* BB if rc = -ETXTBUSY goto the rename logic BB */
  681. }
  682. }
  683. }
  684. if (direntry->d_inode) {
  685. cifsInode = CIFS_I(direntry->d_inode);
  686. cifsInode->time = 0; /* will force revalidate to get info
  687. when needed */
  688. direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
  689. }
  690. if (inode) {
  691. inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
  692. cifsInode = CIFS_I(inode);
  693. cifsInode->time = 0; /* force revalidate of dir as well */
  694. }
  695. kfree(full_path);
  696. FreeXid(xid);
  697. return rc;
  698. }
  699. static void posix_fill_in_inode(struct inode *tmp_inode,
  700. FILE_UNIX_BASIC_INFO *pData, int *pobject_type, int isNewInode)
  701. {
  702. loff_t local_size;
  703. struct timespec local_mtime;
  704. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  705. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  706. __u32 type = le32_to_cpu(pData->Type);
  707. __u64 num_of_bytes = le64_to_cpu(pData->NumOfBytes);
  708. __u64 end_of_file = le64_to_cpu(pData->EndOfFile);
  709. cifsInfo->time = jiffies;
  710. atomic_inc(&cifsInfo->inUse);
  711. /* save mtime and size */
  712. local_mtime = tmp_inode->i_mtime;
  713. local_size = tmp_inode->i_size;
  714. tmp_inode->i_atime =
  715. cifs_NTtimeToUnix(le64_to_cpu(pData->LastAccessTime));
  716. tmp_inode->i_mtime =
  717. cifs_NTtimeToUnix(le64_to_cpu(pData->LastModificationTime));
  718. tmp_inode->i_ctime =
  719. cifs_NTtimeToUnix(le64_to_cpu(pData->LastStatusChange));
  720. tmp_inode->i_mode = le64_to_cpu(pData->Permissions);
  721. /* since we set the inode type below we need to mask off type
  722. to avoid strange results if bits above were corrupt */
  723. tmp_inode->i_mode &= ~S_IFMT;
  724. if (type == UNIX_FILE) {
  725. *pobject_type = DT_REG;
  726. tmp_inode->i_mode |= S_IFREG;
  727. } else if (type == UNIX_SYMLINK) {
  728. *pobject_type = DT_LNK;
  729. tmp_inode->i_mode |= S_IFLNK;
  730. } else if (type == UNIX_DIR) {
  731. *pobject_type = DT_DIR;
  732. tmp_inode->i_mode |= S_IFDIR;
  733. } else if (type == UNIX_CHARDEV) {
  734. *pobject_type = DT_CHR;
  735. tmp_inode->i_mode |= S_IFCHR;
  736. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pData->DevMajor),
  737. le64_to_cpu(pData->DevMinor) & MINORMASK);
  738. } else if (type == UNIX_BLOCKDEV) {
  739. *pobject_type = DT_BLK;
  740. tmp_inode->i_mode |= S_IFBLK;
  741. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pData->DevMajor),
  742. le64_to_cpu(pData->DevMinor) & MINORMASK);
  743. } else if (type == UNIX_FIFO) {
  744. *pobject_type = DT_FIFO;
  745. tmp_inode->i_mode |= S_IFIFO;
  746. } else if (type == UNIX_SOCKET) {
  747. *pobject_type = DT_SOCK;
  748. tmp_inode->i_mode |= S_IFSOCK;
  749. } else {
  750. /* safest to just call it a file */
  751. *pobject_type = DT_REG;
  752. tmp_inode->i_mode |= S_IFREG;
  753. cFYI(1, ("unknown inode type %d", type));
  754. }
  755. #ifdef CONFIG_CIFS_DEBUG2
  756. cFYI(1, ("object type: %d", type));
  757. #endif
  758. tmp_inode->i_uid = le64_to_cpu(pData->Uid);
  759. tmp_inode->i_gid = le64_to_cpu(pData->Gid);
  760. tmp_inode->i_nlink = le64_to_cpu(pData->Nlinks);
  761. spin_lock(&tmp_inode->i_lock);
  762. if (is_size_safe_to_change(cifsInfo, end_of_file)) {
  763. /* can not safely change the file size here if the
  764. client is writing to it due to potential races */
  765. i_size_write(tmp_inode, end_of_file);
  766. /* 512 bytes (2**9) is the fake blocksize that must be used */
  767. /* for this calculation, not the real blocksize */
  768. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  769. }
  770. spin_unlock(&tmp_inode->i_lock);
  771. if (S_ISREG(tmp_inode->i_mode)) {
  772. cFYI(1, ("File inode"));
  773. tmp_inode->i_op = &cifs_file_inode_ops;
  774. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  775. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  776. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  777. else
  778. tmp_inode->i_fop = &cifs_file_direct_ops;
  779. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  780. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  781. else
  782. tmp_inode->i_fop = &cifs_file_ops;
  783. if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  784. (cifs_sb->tcon->ses->server->maxBuf <
  785. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
  786. tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  787. else
  788. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  789. if (isNewInode)
  790. return; /* No sense invalidating pages for new inode
  791. since we we have not started caching
  792. readahead file data yet */
  793. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  794. (local_size == tmp_inode->i_size)) {
  795. cFYI(1, ("inode exists but unchanged"));
  796. } else {
  797. /* file may have changed on server */
  798. cFYI(1, ("invalidate inode, readdir detected change"));
  799. invalidate_remote_inode(tmp_inode);
  800. }
  801. } else if (S_ISDIR(tmp_inode->i_mode)) {
  802. cFYI(1, ("Directory inode"));
  803. tmp_inode->i_op = &cifs_dir_inode_ops;
  804. tmp_inode->i_fop = &cifs_dir_ops;
  805. } else if (S_ISLNK(tmp_inode->i_mode)) {
  806. cFYI(1, ("Symbolic Link inode"));
  807. tmp_inode->i_op = &cifs_symlink_inode_ops;
  808. /* tmp_inode->i_fop = *//* do not need to set to anything */
  809. } else {
  810. cFYI(1, ("Special inode"));
  811. init_special_inode(tmp_inode, tmp_inode->i_mode,
  812. tmp_inode->i_rdev);
  813. }
  814. }
  815. int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
  816. {
  817. int rc = 0;
  818. int xid;
  819. struct cifs_sb_info *cifs_sb;
  820. struct cifsTconInfo *pTcon;
  821. char *full_path = NULL;
  822. struct inode *newinode = NULL;
  823. cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
  824. xid = GetXid();
  825. cifs_sb = CIFS_SB(inode->i_sb);
  826. pTcon = cifs_sb->tcon;
  827. full_path = build_path_from_dentry(direntry);
  828. if (full_path == NULL) {
  829. FreeXid(xid);
  830. return -ENOMEM;
  831. }
  832. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  833. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  834. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  835. u32 oplock = 0;
  836. FILE_UNIX_BASIC_INFO * pInfo =
  837. kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  838. if (pInfo == NULL) {
  839. rc = -ENOMEM;
  840. goto mkdir_out;
  841. }
  842. rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
  843. mode, NULL /* netfid */, pInfo, &oplock,
  844. full_path, cifs_sb->local_nls,
  845. cifs_sb->mnt_cifs_flags &
  846. CIFS_MOUNT_MAP_SPECIAL_CHR);
  847. if (rc) {
  848. cFYI(1, ("posix mkdir returned 0x%x", rc));
  849. d_drop(direntry);
  850. } else {
  851. int obj_type;
  852. if (pInfo->Type == -1) /* no return info - go query */
  853. goto mkdir_get_info;
  854. /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
  855. to set uid/gid */
  856. inc_nlink(inode);
  857. if (pTcon->nocase)
  858. direntry->d_op = &cifs_ci_dentry_ops;
  859. else
  860. direntry->d_op = &cifs_dentry_ops;
  861. newinode = new_inode(inode->i_sb);
  862. if (newinode == NULL)
  863. goto mkdir_get_info;
  864. /* Is an i_ino of zero legal? */
  865. /* Are there sanity checks we can use to ensure that
  866. the server is really filling in that field? */
  867. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  868. newinode->i_ino =
  869. (unsigned long)pInfo->UniqueId;
  870. } /* note ino incremented to unique num in new_inode */
  871. if (inode->i_sb->s_flags & MS_NOATIME)
  872. newinode->i_flags |= S_NOATIME | S_NOCMTIME;
  873. newinode->i_nlink = 2;
  874. insert_inode_hash(newinode);
  875. d_instantiate(direntry, newinode);
  876. /* we already checked in POSIXCreate whether
  877. frame was long enough */
  878. posix_fill_in_inode(direntry->d_inode,
  879. pInfo, &obj_type, 1 /* NewInode */);
  880. #ifdef CONFIG_CIFS_DEBUG2
  881. cFYI(1, ("instantiated dentry %p %s to inode %p",
  882. direntry, direntry->d_name.name, newinode));
  883. if (newinode->i_nlink != 2)
  884. cFYI(1, ("unexpected number of links %d",
  885. newinode->i_nlink));
  886. #endif
  887. }
  888. kfree(pInfo);
  889. goto mkdir_out;
  890. }
  891. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  892. rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
  893. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  894. if (rc) {
  895. cFYI(1, ("cifs_mkdir returned 0x%x", rc));
  896. d_drop(direntry);
  897. } else {
  898. mkdir_get_info:
  899. inc_nlink(inode);
  900. if (pTcon->unix_ext)
  901. rc = cifs_get_inode_info_unix(&newinode, full_path,
  902. inode->i_sb, xid);
  903. else
  904. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  905. inode->i_sb, xid);
  906. if (pTcon->nocase)
  907. direntry->d_op = &cifs_ci_dentry_ops;
  908. else
  909. direntry->d_op = &cifs_dentry_ops;
  910. d_instantiate(direntry, newinode);
  911. /* setting nlink not necessary except in cases where we
  912. * failed to get it from the server or was set bogus */
  913. if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
  914. direntry->d_inode->i_nlink = 2;
  915. if (pTcon->unix_ext) {
  916. mode &= ~current->fs->umask;
  917. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  918. CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  919. mode,
  920. (__u64)current->fsuid,
  921. (__u64)current->fsgid,
  922. 0 /* dev_t */,
  923. cifs_sb->local_nls,
  924. cifs_sb->mnt_cifs_flags &
  925. CIFS_MOUNT_MAP_SPECIAL_CHR);
  926. } else {
  927. CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  928. mode, (__u64)-1,
  929. (__u64)-1, 0 /* dev_t */,
  930. cifs_sb->local_nls,
  931. cifs_sb->mnt_cifs_flags &
  932. CIFS_MOUNT_MAP_SPECIAL_CHR);
  933. }
  934. } else {
  935. /* BB to be implemented via Windows secrty descriptors
  936. eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
  937. -1, -1, local_nls); */
  938. if (direntry->d_inode) {
  939. direntry->d_inode->i_mode = mode;
  940. direntry->d_inode->i_mode |= S_IFDIR;
  941. if (cifs_sb->mnt_cifs_flags &
  942. CIFS_MOUNT_SET_UID) {
  943. direntry->d_inode->i_uid =
  944. current->fsuid;
  945. direntry->d_inode->i_gid =
  946. current->fsgid;
  947. }
  948. }
  949. }
  950. }
  951. mkdir_out:
  952. kfree(full_path);
  953. FreeXid(xid);
  954. return rc;
  955. }
  956. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  957. {
  958. int rc = 0;
  959. int xid;
  960. struct cifs_sb_info *cifs_sb;
  961. struct cifsTconInfo *pTcon;
  962. char *full_path = NULL;
  963. struct cifsInodeInfo *cifsInode;
  964. cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
  965. xid = GetXid();
  966. cifs_sb = CIFS_SB(inode->i_sb);
  967. pTcon = cifs_sb->tcon;
  968. full_path = build_path_from_dentry(direntry);
  969. if (full_path == NULL) {
  970. FreeXid(xid);
  971. return -ENOMEM;
  972. }
  973. rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
  974. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  975. if (!rc) {
  976. drop_nlink(inode);
  977. spin_lock(&direntry->d_inode->i_lock);
  978. i_size_write(direntry->d_inode, 0);
  979. clear_nlink(direntry->d_inode);
  980. spin_unlock(&direntry->d_inode->i_lock);
  981. }
  982. cifsInode = CIFS_I(direntry->d_inode);
  983. cifsInode->time = 0; /* force revalidate to go get info when
  984. needed */
  985. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  986. current_fs_time(inode->i_sb);
  987. kfree(full_path);
  988. FreeXid(xid);
  989. return rc;
  990. }
  991. int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
  992. struct inode *target_inode, struct dentry *target_direntry)
  993. {
  994. char *fromName;
  995. char *toName;
  996. struct cifs_sb_info *cifs_sb_source;
  997. struct cifs_sb_info *cifs_sb_target;
  998. struct cifsTconInfo *pTcon;
  999. int xid;
  1000. int rc = 0;
  1001. xid = GetXid();
  1002. cifs_sb_target = CIFS_SB(target_inode->i_sb);
  1003. cifs_sb_source = CIFS_SB(source_inode->i_sb);
  1004. pTcon = cifs_sb_source->tcon;
  1005. if (pTcon != cifs_sb_target->tcon) {
  1006. FreeXid(xid);
  1007. return -EXDEV; /* BB actually could be allowed if same server,
  1008. but different share.
  1009. Might eventually add support for this */
  1010. }
  1011. /* we already have the rename sem so we do not need to grab it again
  1012. here to protect the path integrity */
  1013. fromName = build_path_from_dentry(source_direntry);
  1014. toName = build_path_from_dentry(target_direntry);
  1015. if ((fromName == NULL) || (toName == NULL)) {
  1016. rc = -ENOMEM;
  1017. goto cifs_rename_exit;
  1018. }
  1019. rc = CIFSSMBRename(xid, pTcon, fromName, toName,
  1020. cifs_sb_source->local_nls,
  1021. cifs_sb_source->mnt_cifs_flags &
  1022. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1023. if (rc == -EEXIST) {
  1024. /* check if they are the same file because rename of hardlinked
  1025. files is a noop */
  1026. FILE_UNIX_BASIC_INFO *info_buf_source;
  1027. FILE_UNIX_BASIC_INFO *info_buf_target;
  1028. info_buf_source =
  1029. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1030. if (info_buf_source != NULL) {
  1031. info_buf_target = info_buf_source + 1;
  1032. if (pTcon->unix_ext)
  1033. rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
  1034. info_buf_source,
  1035. cifs_sb_source->local_nls,
  1036. cifs_sb_source->mnt_cifs_flags &
  1037. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1038. /* else rc is still EEXIST so will fall through to
  1039. unlink the target and retry rename */
  1040. if (rc == 0) {
  1041. rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
  1042. info_buf_target,
  1043. cifs_sb_target->local_nls,
  1044. /* remap based on source sb */
  1045. cifs_sb_source->mnt_cifs_flags &
  1046. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1047. }
  1048. if ((rc == 0) &&
  1049. (info_buf_source->UniqueId ==
  1050. info_buf_target->UniqueId)) {
  1051. /* do not rename since the files are hardlinked which
  1052. is a noop */
  1053. } else {
  1054. /* we either can not tell the files are hardlinked
  1055. (as with Windows servers) or files are not
  1056. hardlinked so delete the target manually before
  1057. renaming to follow POSIX rather than Windows
  1058. semantics */
  1059. cifs_unlink(target_inode, target_direntry);
  1060. rc = CIFSSMBRename(xid, pTcon, fromName,
  1061. toName,
  1062. cifs_sb_source->local_nls,
  1063. cifs_sb_source->mnt_cifs_flags
  1064. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1065. }
  1066. kfree(info_buf_source);
  1067. } /* if we can not get memory just leave rc as EEXIST */
  1068. }
  1069. if (rc) {
  1070. cFYI(1, ("rename rc %d", rc));
  1071. }
  1072. if ((rc == -EIO) || (rc == -EEXIST)) {
  1073. int oplock = FALSE;
  1074. __u16 netfid;
  1075. /* BB FIXME Is Generic Read correct for rename? */
  1076. /* if renaming directory - we should not say CREATE_NOT_DIR,
  1077. need to test renaming open directory, also GENERIC_READ
  1078. might not right be right access to request */
  1079. rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
  1080. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  1081. cifs_sb_source->local_nls,
  1082. cifs_sb_source->mnt_cifs_flags &
  1083. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1084. if (rc == 0) {
  1085. rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
  1086. cifs_sb_source->local_nls,
  1087. cifs_sb_source->mnt_cifs_flags &
  1088. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1089. CIFSSMBClose(xid, pTcon, netfid);
  1090. }
  1091. }
  1092. cifs_rename_exit:
  1093. kfree(fromName);
  1094. kfree(toName);
  1095. FreeXid(xid);
  1096. return rc;
  1097. }
  1098. int cifs_revalidate(struct dentry *direntry)
  1099. {
  1100. int xid;
  1101. int rc = 0;
  1102. char *full_path;
  1103. struct cifs_sb_info *cifs_sb;
  1104. struct cifsInodeInfo *cifsInode;
  1105. loff_t local_size;
  1106. struct timespec local_mtime;
  1107. int invalidate_inode = FALSE;
  1108. if (direntry->d_inode == NULL)
  1109. return -ENOENT;
  1110. cifsInode = CIFS_I(direntry->d_inode);
  1111. if (cifsInode == NULL)
  1112. return -ENOENT;
  1113. /* no sense revalidating inode info on file that no one can write */
  1114. if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
  1115. return rc;
  1116. xid = GetXid();
  1117. cifs_sb = CIFS_SB(direntry->d_sb);
  1118. /* can not safely grab the rename sem here if rename calls revalidate
  1119. since that would deadlock */
  1120. full_path = build_path_from_dentry(direntry);
  1121. if (full_path == NULL) {
  1122. FreeXid(xid);
  1123. return -ENOMEM;
  1124. }
  1125. cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
  1126. "jiffies %ld", full_path, direntry->d_inode,
  1127. direntry->d_inode->i_count.counter, direntry,
  1128. direntry->d_time, jiffies));
  1129. if (cifsInode->time == 0) {
  1130. /* was set to zero previously to force revalidate */
  1131. } else if (time_before(jiffies, cifsInode->time + HZ) &&
  1132. lookupCacheEnabled) {
  1133. if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
  1134. (direntry->d_inode->i_nlink == 1)) {
  1135. kfree(full_path);
  1136. FreeXid(xid);
  1137. return rc;
  1138. } else {
  1139. cFYI(1, ("Have to revalidate file due to hardlinks"));
  1140. }
  1141. }
  1142. /* save mtime and size */
  1143. local_mtime = direntry->d_inode->i_mtime;
  1144. local_size = direntry->d_inode->i_size;
  1145. if (cifs_sb->tcon->unix_ext) {
  1146. rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
  1147. direntry->d_sb, xid);
  1148. if (rc) {
  1149. cFYI(1, ("error on getting revalidate info %d", rc));
  1150. /* if (rc != -ENOENT)
  1151. rc = 0; */ /* BB should we cache info on
  1152. certain errors? */
  1153. }
  1154. } else {
  1155. rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
  1156. direntry->d_sb, xid);
  1157. if (rc) {
  1158. cFYI(1, ("error on getting revalidate info %d", rc));
  1159. /* if (rc != -ENOENT)
  1160. rc = 0; */ /* BB should we cache info on
  1161. certain errors? */
  1162. }
  1163. }
  1164. /* should we remap certain errors, access denied?, to zero */
  1165. /* if not oplocked, we invalidate inode pages if mtime or file size
  1166. had changed on server */
  1167. if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
  1168. (local_size == direntry->d_inode->i_size)) {
  1169. cFYI(1, ("cifs_revalidate - inode unchanged"));
  1170. } else {
  1171. /* file may have changed on server */
  1172. if (cifsInode->clientCanCacheRead) {
  1173. /* no need to invalidate inode pages since we were the
  1174. only ones who could have modified the file and the
  1175. server copy is staler than ours */
  1176. } else {
  1177. invalidate_inode = TRUE;
  1178. }
  1179. }
  1180. /* can not grab this sem since kernel filesys locking documentation
  1181. indicates i_mutex may be taken by the kernel on lookup and rename
  1182. which could deadlock if we grab the i_mutex here as well */
  1183. /* mutex_lock(&direntry->d_inode->i_mutex);*/
  1184. /* need to write out dirty pages here */
  1185. if (direntry->d_inode->i_mapping) {
  1186. /* do we need to lock inode until after invalidate completes
  1187. below? */
  1188. filemap_fdatawrite(direntry->d_inode->i_mapping);
  1189. }
  1190. if (invalidate_inode) {
  1191. /* shrink_dcache not necessary now that cifs dentry ops
  1192. are exported for negative dentries */
  1193. /* if (S_ISDIR(direntry->d_inode->i_mode))
  1194. shrink_dcache_parent(direntry); */
  1195. if (S_ISREG(direntry->d_inode->i_mode)) {
  1196. if (direntry->d_inode->i_mapping)
  1197. filemap_fdatawait(direntry->d_inode->i_mapping);
  1198. /* may eventually have to do this for open files too */
  1199. if (list_empty(&(cifsInode->openFileList))) {
  1200. /* changed on server - flush read ahead pages */
  1201. cFYI(1, ("Invalidating read ahead data on "
  1202. "closed file"));
  1203. invalidate_remote_inode(direntry->d_inode);
  1204. }
  1205. }
  1206. }
  1207. /* mutex_unlock(&direntry->d_inode->i_mutex); */
  1208. kfree(full_path);
  1209. FreeXid(xid);
  1210. return rc;
  1211. }
  1212. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1213. struct kstat *stat)
  1214. {
  1215. int err = cifs_revalidate(dentry);
  1216. if (!err) {
  1217. generic_fillattr(dentry->d_inode, stat);
  1218. stat->blksize = CIFS_MAX_MSGSIZE;
  1219. }
  1220. return err;
  1221. }
  1222. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1223. {
  1224. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1225. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1226. struct page *page;
  1227. int rc = 0;
  1228. page = grab_cache_page(mapping, index);
  1229. if (!page)
  1230. return -ENOMEM;
  1231. zero_user_page(page, offset, PAGE_CACHE_SIZE - offset, KM_USER0);
  1232. unlock_page(page);
  1233. page_cache_release(page);
  1234. return rc;
  1235. }
  1236. static int cifs_vmtruncate(struct inode *inode, loff_t offset)
  1237. {
  1238. struct address_space *mapping = inode->i_mapping;
  1239. unsigned long limit;
  1240. spin_lock(&inode->i_lock);
  1241. if (inode->i_size < offset)
  1242. goto do_expand;
  1243. /*
  1244. * truncation of in-use swapfiles is disallowed - it would cause
  1245. * subsequent swapout to scribble on the now-freed blocks.
  1246. */
  1247. if (IS_SWAPFILE(inode)) {
  1248. spin_unlock(&inode->i_lock);
  1249. goto out_busy;
  1250. }
  1251. i_size_write(inode, offset);
  1252. spin_unlock(&inode->i_lock);
  1253. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1254. truncate_inode_pages(mapping, offset);
  1255. goto out_truncate;
  1256. do_expand:
  1257. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1258. if (limit != RLIM_INFINITY && offset > limit) {
  1259. spin_unlock(&inode->i_lock);
  1260. goto out_sig;
  1261. }
  1262. if (offset > inode->i_sb->s_maxbytes) {
  1263. spin_unlock(&inode->i_lock);
  1264. goto out_big;
  1265. }
  1266. i_size_write(inode, offset);
  1267. spin_unlock(&inode->i_lock);
  1268. out_truncate:
  1269. if (inode->i_op && inode->i_op->truncate)
  1270. inode->i_op->truncate(inode);
  1271. return 0;
  1272. out_sig:
  1273. send_sig(SIGXFSZ, current, 0);
  1274. out_big:
  1275. return -EFBIG;
  1276. out_busy:
  1277. return -ETXTBSY;
  1278. }
  1279. int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1280. {
  1281. int xid;
  1282. struct cifs_sb_info *cifs_sb;
  1283. struct cifsTconInfo *pTcon;
  1284. char *full_path = NULL;
  1285. int rc = -EACCES;
  1286. struct cifsFileInfo *open_file = NULL;
  1287. FILE_BASIC_INFO time_buf;
  1288. int set_time = FALSE;
  1289. int set_dosattr = FALSE;
  1290. __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
  1291. __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
  1292. __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
  1293. struct cifsInodeInfo *cifsInode;
  1294. xid = GetXid();
  1295. cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
  1296. direntry->d_name.name, attrs->ia_valid));
  1297. cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
  1298. pTcon = cifs_sb->tcon;
  1299. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1300. /* check if we have permission to change attrs */
  1301. rc = inode_change_ok(direntry->d_inode, attrs);
  1302. if (rc < 0) {
  1303. FreeXid(xid);
  1304. return rc;
  1305. } else
  1306. rc = 0;
  1307. }
  1308. full_path = build_path_from_dentry(direntry);
  1309. if (full_path == NULL) {
  1310. FreeXid(xid);
  1311. return -ENOMEM;
  1312. }
  1313. cifsInode = CIFS_I(direntry->d_inode);
  1314. /* BB check if we need to refresh inode from server now ? BB */
  1315. /* need to flush data before changing file size on server */
  1316. filemap_write_and_wait(direntry->d_inode->i_mapping);
  1317. if (attrs->ia_valid & ATTR_SIZE) {
  1318. /* To avoid spurious oplock breaks from server, in the case of
  1319. inodes that we already have open, avoid doing path based
  1320. setting of file size if we can do it by handle.
  1321. This keeps our caching token (oplock) and avoids timeouts
  1322. when the local oplock break takes longer to flush
  1323. writebehind data than the SMB timeout for the SetPathInfo
  1324. request would allow */
  1325. open_file = find_writable_file(cifsInode);
  1326. if (open_file) {
  1327. __u16 nfid = open_file->netfid;
  1328. __u32 npid = open_file->pid;
  1329. rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
  1330. nfid, npid, FALSE);
  1331. atomic_dec(&open_file->wrtPending);
  1332. cFYI(1, ("SetFSize for attrs rc = %d", rc));
  1333. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1334. int bytes_written;
  1335. rc = CIFSSMBWrite(xid, pTcon,
  1336. nfid, 0, attrs->ia_size,
  1337. &bytes_written, NULL, NULL,
  1338. 1 /* 45 seconds */);
  1339. cFYI(1, ("Wrt seteof rc %d", rc));
  1340. }
  1341. } else
  1342. rc = -EINVAL;
  1343. if (rc != 0) {
  1344. /* Set file size by pathname rather than by handle
  1345. either because no valid, writeable file handle for
  1346. it was found or because there was an error setting
  1347. it by handle */
  1348. rc = CIFSSMBSetEOF(xid, pTcon, full_path,
  1349. attrs->ia_size, FALSE,
  1350. cifs_sb->local_nls,
  1351. cifs_sb->mnt_cifs_flags &
  1352. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1353. cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
  1354. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1355. __u16 netfid;
  1356. int oplock = FALSE;
  1357. rc = SMBLegacyOpen(xid, pTcon, full_path,
  1358. FILE_OPEN,
  1359. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  1360. CREATE_NOT_DIR, &netfid, &oplock,
  1361. NULL, cifs_sb->local_nls,
  1362. cifs_sb->mnt_cifs_flags &
  1363. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1364. if (rc == 0) {
  1365. int bytes_written;
  1366. rc = CIFSSMBWrite(xid, pTcon,
  1367. netfid, 0,
  1368. attrs->ia_size,
  1369. &bytes_written, NULL,
  1370. NULL, 1 /* 45 sec */);
  1371. cFYI(1, ("wrt seteof rc %d", rc));
  1372. CIFSSMBClose(xid, pTcon, netfid);
  1373. }
  1374. }
  1375. }
  1376. /* Server is ok setting allocation size implicitly - no need
  1377. to call:
  1378. CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
  1379. cifs_sb->local_nls);
  1380. */
  1381. if (rc == 0) {
  1382. rc = cifs_vmtruncate(direntry->d_inode, attrs->ia_size);
  1383. cifs_truncate_page(direntry->d_inode->i_mapping,
  1384. direntry->d_inode->i_size);
  1385. } else
  1386. goto cifs_setattr_exit;
  1387. }
  1388. if (attrs->ia_valid & ATTR_UID) {
  1389. cFYI(1, ("UID changed to %d", attrs->ia_uid));
  1390. uid = attrs->ia_uid;
  1391. }
  1392. if (attrs->ia_valid & ATTR_GID) {
  1393. cFYI(1, ("GID changed to %d", attrs->ia_gid));
  1394. gid = attrs->ia_gid;
  1395. }
  1396. time_buf.Attributes = 0;
  1397. if (attrs->ia_valid & ATTR_MODE) {
  1398. cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
  1399. mode = attrs->ia_mode;
  1400. }
  1401. if ((pTcon->unix_ext)
  1402. && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
  1403. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
  1404. 0 /* dev_t */, cifs_sb->local_nls,
  1405. cifs_sb->mnt_cifs_flags &
  1406. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1407. else if (attrs->ia_valid & ATTR_MODE) {
  1408. rc = 0;
  1409. if ((mode & S_IWUGO) == 0) /* not writeable */ {
  1410. if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  1411. set_dosattr = TRUE;
  1412. time_buf.Attributes =
  1413. cpu_to_le32(cifsInode->cifsAttrs |
  1414. ATTR_READONLY);
  1415. }
  1416. } else if (cifsInode->cifsAttrs & ATTR_READONLY) {
  1417. /* If file is readonly on server, we would
  1418. not be able to write to it - so if any write
  1419. bit is enabled for user or group or other we
  1420. need to at least try to remove r/o dos attr */
  1421. set_dosattr = TRUE;
  1422. time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
  1423. (~ATTR_READONLY));
  1424. /* Windows ignores set to zero */
  1425. if (time_buf.Attributes == 0)
  1426. time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
  1427. }
  1428. /* BB to be implemented -
  1429. via Windows security descriptors or streams */
  1430. /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
  1431. cifs_sb->local_nls); */
  1432. }
  1433. if (attrs->ia_valid & ATTR_ATIME) {
  1434. set_time = TRUE;
  1435. time_buf.LastAccessTime =
  1436. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1437. } else
  1438. time_buf.LastAccessTime = 0;
  1439. if (attrs->ia_valid & ATTR_MTIME) {
  1440. set_time = TRUE;
  1441. time_buf.LastWriteTime =
  1442. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1443. } else
  1444. time_buf.LastWriteTime = 0;
  1445. /* Do not set ctime explicitly unless other time
  1446. stamps are changed explicitly (i.e. by utime()
  1447. since we would then have a mix of client and
  1448. server times */
  1449. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1450. set_time = TRUE;
  1451. /* Although Samba throws this field away
  1452. it may be useful to Windows - but we do
  1453. not want to set ctime unless some other
  1454. timestamp is changing */
  1455. cFYI(1, ("CIFS - CTIME changed"));
  1456. time_buf.ChangeTime =
  1457. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1458. } else
  1459. time_buf.ChangeTime = 0;
  1460. if (set_time || set_dosattr) {
  1461. time_buf.CreationTime = 0; /* do not change */
  1462. /* In the future we should experiment - try setting timestamps
  1463. via Handle (SetFileInfo) instead of by path */
  1464. if (!(pTcon->ses->flags & CIFS_SES_NT4))
  1465. rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
  1466. cifs_sb->local_nls,
  1467. cifs_sb->mnt_cifs_flags &
  1468. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1469. else
  1470. rc = -EOPNOTSUPP;
  1471. if (rc == -EOPNOTSUPP) {
  1472. int oplock = FALSE;
  1473. __u16 netfid;
  1474. cFYI(1, ("calling SetFileInfo since SetPathInfo for "
  1475. "times not supported by this server"));
  1476. /* BB we could scan to see if we already have it open
  1477. and pass in pid of opener to function */
  1478. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
  1479. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  1480. CREATE_NOT_DIR, &netfid, &oplock,
  1481. NULL, cifs_sb->local_nls,
  1482. cifs_sb->mnt_cifs_flags &
  1483. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1484. if (rc == 0) {
  1485. rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
  1486. netfid);
  1487. CIFSSMBClose(xid, pTcon, netfid);
  1488. } else {
  1489. /* BB For even older servers we could convert time_buf
  1490. into old DOS style which uses two second
  1491. granularity */
  1492. /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
  1493. &time_buf, cifs_sb->local_nls); */
  1494. }
  1495. }
  1496. /* Even if error on time set, no sense failing the call if
  1497. the server would set the time to a reasonable value anyway,
  1498. and this check ensures that we are not being called from
  1499. sys_utimes in which case we ought to fail the call back to
  1500. the user when the server rejects the call */
  1501. if ((rc) && (attrs->ia_valid &
  1502. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1503. rc = 0;
  1504. }
  1505. /* do not need local check to inode_check_ok since the server does
  1506. that */
  1507. if (!rc)
  1508. rc = inode_setattr(direntry->d_inode, attrs);
  1509. cifs_setattr_exit:
  1510. kfree(full_path);
  1511. FreeXid(xid);
  1512. return rc;
  1513. }
  1514. #if 0
  1515. void cifs_delete_inode(struct inode *inode)
  1516. {
  1517. cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
  1518. /* may have to add back in if and when safe distributed caching of
  1519. directories added e.g. via FindNotify */
  1520. }
  1521. #endif