inode.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2005
  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. insert_inode_hash(*pinode);
  90. }
  91. inode = *pinode;
  92. cifsInfo = CIFS_I(inode);
  93. cFYI(1, ("Old time %ld", cifsInfo->time));
  94. cifsInfo->time = jiffies;
  95. cFYI(1, ("New time %ld", cifsInfo->time));
  96. /* this is ok to set on every inode revalidate */
  97. atomic_set(&cifsInfo->inUse,1);
  98. inode->i_atime =
  99. cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
  100. inode->i_mtime =
  101. cifs_NTtimeToUnix(le64_to_cpu
  102. (findData.LastModificationTime));
  103. inode->i_ctime =
  104. cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
  105. inode->i_mode = le64_to_cpu(findData.Permissions);
  106. /* since we set the inode type below we need to mask off
  107. to avoid strange results if bits set above */
  108. inode->i_mode &= ~S_IFMT;
  109. if (type == UNIX_FILE) {
  110. inode->i_mode |= S_IFREG;
  111. } else if (type == UNIX_SYMLINK) {
  112. inode->i_mode |= S_IFLNK;
  113. } else if (type == UNIX_DIR) {
  114. inode->i_mode |= S_IFDIR;
  115. } else if (type == UNIX_CHARDEV) {
  116. inode->i_mode |= S_IFCHR;
  117. inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
  118. le64_to_cpu(findData.DevMinor) & MINORMASK);
  119. } else if (type == UNIX_BLOCKDEV) {
  120. inode->i_mode |= S_IFBLK;
  121. inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
  122. le64_to_cpu(findData.DevMinor) & MINORMASK);
  123. } else if (type == UNIX_FIFO) {
  124. inode->i_mode |= S_IFIFO;
  125. } else if (type == UNIX_SOCKET) {
  126. inode->i_mode |= S_IFSOCK;
  127. } else {
  128. /* safest to call it a file if we do not know */
  129. inode->i_mode |= S_IFREG;
  130. cFYI(1,("unknown type %d",type));
  131. }
  132. inode->i_uid = le64_to_cpu(findData.Uid);
  133. inode->i_gid = le64_to_cpu(findData.Gid);
  134. inode->i_nlink = le64_to_cpu(findData.Nlinks);
  135. if (is_size_safe_to_change(cifsInfo)) {
  136. /* can not safely change the file size here if the
  137. client is writing to it due to potential races */
  138. i_size_write(inode, end_of_file);
  139. /* blksize needs to be multiple of two. So safer to default to
  140. blksize and blkbits set in superblock so 2**blkbits and blksize
  141. will match rather than setting to:
  142. (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
  143. /* This seems incredibly stupid but it turns out that i_blocks
  144. is not related to (i_size / i_blksize), instead 512 byte size
  145. is required for calculating num blocks */
  146. /* 512 bytes (2**9) is the fake blocksize that must be used */
  147. /* for this calculation */
  148. inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  149. }
  150. if (num_of_bytes < end_of_file)
  151. cFYI(1, ("allocation size less than end of file"));
  152. cFYI(1, ("Size %ld and blocks %llu",
  153. (unsigned long) inode->i_size,
  154. (unsigned long long)inode->i_blocks));
  155. if (S_ISREG(inode->i_mode)) {
  156. cFYI(1, ("File inode"));
  157. inode->i_op = &cifs_file_inode_ops;
  158. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  159. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  160. inode->i_fop =
  161. &cifs_file_direct_nobrl_ops;
  162. else
  163. inode->i_fop = &cifs_file_direct_ops;
  164. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  165. inode->i_fop = &cifs_file_nobrl_ops;
  166. else /* not direct, send byte range locks */
  167. inode->i_fop = &cifs_file_ops;
  168. /* check if server can support readpages */
  169. if(pTcon->ses->server->maxBuf <
  170. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  171. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  172. else
  173. inode->i_data.a_ops = &cifs_addr_ops;
  174. } else if (S_ISDIR(inode->i_mode)) {
  175. cFYI(1, ("Directory inode"));
  176. inode->i_op = &cifs_dir_inode_ops;
  177. inode->i_fop = &cifs_dir_ops;
  178. } else if (S_ISLNK(inode->i_mode)) {
  179. cFYI(1, ("Symbolic Link inode"));
  180. inode->i_op = &cifs_symlink_inode_ops;
  181. /* tmp_inode->i_fop = */ /* do not need to set to anything */
  182. } else {
  183. cFYI(1, ("Init special inode"));
  184. init_special_inode(inode, inode->i_mode,
  185. inode->i_rdev);
  186. }
  187. }
  188. return rc;
  189. }
  190. static int decode_sfu_inode(struct inode * inode, __u64 size,
  191. const unsigned char *path,
  192. struct cifs_sb_info *cifs_sb, int xid)
  193. {
  194. int rc;
  195. int oplock = FALSE;
  196. __u16 netfid;
  197. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  198. char buf[24];
  199. unsigned int bytes_read;
  200. char * pbuf;
  201. pbuf = buf;
  202. if(size == 0) {
  203. inode->i_mode |= S_IFIFO;
  204. return 0;
  205. } else if (size < 8) {
  206. return -EINVAL; /* EOPNOTSUPP? */
  207. }
  208. rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
  209. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  210. cifs_sb->local_nls,
  211. cifs_sb->mnt_cifs_flags &
  212. CIFS_MOUNT_MAP_SPECIAL_CHR);
  213. if (rc==0) {
  214. int buf_type = CIFS_NO_BUFFER;
  215. /* Read header */
  216. rc = CIFSSMBRead(xid, pTcon,
  217. netfid,
  218. 24 /* length */, 0 /* offset */,
  219. &bytes_read, &pbuf, &buf_type);
  220. if((rc == 0) && (bytes_read >= 8)) {
  221. if(memcmp("IntxBLK", pbuf, 8) == 0) {
  222. cFYI(1,("Block device"));
  223. inode->i_mode |= S_IFBLK;
  224. if(bytes_read == 24) {
  225. /* we have enough to decode dev num */
  226. __u64 mjr; /* major */
  227. __u64 mnr; /* minor */
  228. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  229. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  230. inode->i_rdev = MKDEV(mjr, mnr);
  231. }
  232. } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
  233. cFYI(1,("Char device"));
  234. inode->i_mode |= S_IFCHR;
  235. if(bytes_read == 24) {
  236. /* we have enough to decode dev num */
  237. __u64 mjr; /* major */
  238. __u64 mnr; /* minor */
  239. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  240. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  241. inode->i_rdev = MKDEV(mjr, mnr);
  242. }
  243. } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
  244. cFYI(1,("Symlink"));
  245. inode->i_mode |= S_IFLNK;
  246. } else {
  247. inode->i_mode |= S_IFREG; /* file? */
  248. rc = -EOPNOTSUPP;
  249. }
  250. } else {
  251. inode->i_mode |= S_IFREG; /* then it is a file */
  252. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  253. }
  254. CIFSSMBClose(xid, pTcon, netfid);
  255. }
  256. return rc;
  257. }
  258. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  259. static int get_sfu_uid_mode(struct inode * inode,
  260. const unsigned char *path,
  261. struct cifs_sb_info *cifs_sb, int xid)
  262. {
  263. #ifdef CONFIG_CIFS_XATTR
  264. ssize_t rc;
  265. char ea_value[4];
  266. __u32 mode;
  267. rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
  268. ea_value, 4 /* size of buf */, cifs_sb->local_nls,
  269. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  270. if(rc < 0)
  271. return (int)rc;
  272. else if (rc > 3) {
  273. mode = le32_to_cpu(*((__le32 *)ea_value));
  274. inode->i_mode &= ~SFBITS_MASK;
  275. cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
  276. inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
  277. cFYI(1,("special mode bits 0%o", mode));
  278. return 0;
  279. } else {
  280. return 0;
  281. }
  282. #else
  283. return -EOPNOTSUPP;
  284. #endif
  285. }
  286. int cifs_get_inode_info(struct inode **pinode,
  287. const unsigned char *search_path, FILE_ALL_INFO *pfindData,
  288. struct super_block *sb, int xid)
  289. {
  290. int rc = 0;
  291. struct cifsTconInfo *pTcon;
  292. struct inode *inode;
  293. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  294. char *tmp_path;
  295. char *buf = NULL;
  296. int adjustTZ = FALSE;
  297. pTcon = cifs_sb->tcon;
  298. cFYI(1,("Getting info on %s", search_path));
  299. if ((pfindData == NULL) && (*pinode != NULL)) {
  300. if (CIFS_I(*pinode)->clientCanCacheRead) {
  301. cFYI(1,("No need to revalidate cached inode sizes"));
  302. return rc;
  303. }
  304. }
  305. /* if file info not passed in then get it from server */
  306. if (pfindData == NULL) {
  307. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  308. if (buf == NULL)
  309. return -ENOMEM;
  310. pfindData = (FILE_ALL_INFO *)buf;
  311. /* could do find first instead but this returns more info */
  312. rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
  313. 0 /* not legacy */,
  314. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  315. CIFS_MOUNT_MAP_SPECIAL_CHR);
  316. /* BB optimize code so we do not make the above call
  317. when server claims no NT SMB support and the above call
  318. failed at least once - set flag in tcon or mount */
  319. if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
  320. rc = SMBQueryInformation(xid, pTcon, search_path,
  321. pfindData, cifs_sb->local_nls,
  322. cifs_sb->mnt_cifs_flags &
  323. CIFS_MOUNT_MAP_SPECIAL_CHR);
  324. adjustTZ = TRUE;
  325. }
  326. }
  327. /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
  328. if (rc) {
  329. if (rc == -EREMOTE) {
  330. tmp_path =
  331. kmalloc(strnlen
  332. (pTcon->treeName,
  333. MAX_TREE_SIZE + 1) +
  334. strnlen(search_path, MAX_PATHCONF) + 1,
  335. GFP_KERNEL);
  336. if (tmp_path == NULL) {
  337. kfree(buf);
  338. return -ENOMEM;
  339. }
  340. strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
  341. strncat(tmp_path, search_path, MAX_PATHCONF);
  342. rc = connect_to_dfs_path(xid, pTcon->ses,
  343. /* treename + */ tmp_path,
  344. cifs_sb->local_nls,
  345. cifs_sb->mnt_cifs_flags &
  346. CIFS_MOUNT_MAP_SPECIAL_CHR);
  347. kfree(tmp_path);
  348. /* BB fix up inode etc. */
  349. } else if (rc) {
  350. kfree(buf);
  351. return rc;
  352. }
  353. } else {
  354. struct cifsInodeInfo *cifsInfo;
  355. __u32 attr = le32_to_cpu(pfindData->Attributes);
  356. /* get new inode */
  357. if (*pinode == NULL) {
  358. *pinode = new_inode(sb);
  359. if (*pinode == NULL) {
  360. kfree(buf);
  361. return -ENOMEM;
  362. }
  363. /* Is an i_ino of zero legal? Can we use that to check
  364. if the server supports returning inode numbers? Are
  365. there other sanity checks we can use to ensure that
  366. the server is really filling in that field? */
  367. /* We can not use the IndexNumber field by default from
  368. Windows or Samba (in ALL_INFO buf) but we can request
  369. it explicitly. It may not be unique presumably if
  370. the server has multiple devices mounted under one
  371. share */
  372. /* There may be higher info levels that work but are
  373. there Windows server or network appliances for which
  374. IndexNumber field is not guaranteed unique? */
  375. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
  376. int rc1 = 0;
  377. __u64 inode_num;
  378. rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
  379. search_path, &inode_num,
  380. cifs_sb->local_nls,
  381. cifs_sb->mnt_cifs_flags &
  382. CIFS_MOUNT_MAP_SPECIAL_CHR);
  383. if (rc1) {
  384. cFYI(1,("GetSrvInodeNum rc %d", rc1));
  385. /* BB EOPNOSUPP disable SERVER_INUM? */
  386. } else /* do we need cast or hash to ino? */
  387. (*pinode)->i_ino = inode_num;
  388. } /* else ino incremented to unique num in new_inode*/
  389. insert_inode_hash(*pinode);
  390. }
  391. inode = *pinode;
  392. cifsInfo = CIFS_I(inode);
  393. cifsInfo->cifsAttrs = attr;
  394. cFYI(1, ("Old time %ld", cifsInfo->time));
  395. cifsInfo->time = jiffies;
  396. cFYI(1, ("New time %ld", cifsInfo->time));
  397. /* blksize needs to be multiple of two. So safer to default to
  398. blksize and blkbits set in superblock so 2**blkbits and blksize
  399. will match rather than setting to:
  400. (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
  401. /* Linux can not store file creation time so ignore it */
  402. if(pfindData->LastAccessTime)
  403. inode->i_atime = cifs_NTtimeToUnix
  404. (le64_to_cpu(pfindData->LastAccessTime));
  405. else /* do not need to use current_fs_time - time not stored */
  406. inode->i_atime = CURRENT_TIME;
  407. inode->i_mtime =
  408. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  409. inode->i_ctime =
  410. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  411. cFYI(0, ("Attributes came in as 0x%x", attr));
  412. if(adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
  413. inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
  414. inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
  415. }
  416. /* set default mode. will override for dirs below */
  417. if (atomic_read(&cifsInfo->inUse) == 0)
  418. /* new inode, can safely set these fields */
  419. inode->i_mode = cifs_sb->mnt_file_mode;
  420. else /* since we set the inode type below we need to mask off
  421. to avoid strange results if type changes and both get orred in */
  422. inode->i_mode &= ~S_IFMT;
  423. /* if (attr & ATTR_REPARSE) */
  424. /* We no longer handle these as symlinks because we could not
  425. follow them due to the absolute path with drive letter */
  426. if (attr & ATTR_DIRECTORY) {
  427. /* override default perms since we do not do byte range locking
  428. on dirs */
  429. inode->i_mode = cifs_sb->mnt_dir_mode;
  430. inode->i_mode |= S_IFDIR;
  431. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  432. (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
  433. /* No need to le64 convert size of zero */
  434. (pfindData->EndOfFile == 0)) {
  435. inode->i_mode = cifs_sb->mnt_file_mode;
  436. inode->i_mode |= S_IFIFO;
  437. /* BB Finish for SFU style symlinks and devices */
  438. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  439. (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
  440. if (decode_sfu_inode(inode,
  441. le64_to_cpu(pfindData->EndOfFile),
  442. search_path,
  443. cifs_sb, xid)) {
  444. cFYI(1,("Unrecognized sfu inode type"));
  445. }
  446. cFYI(1,("sfu mode 0%o",inode->i_mode));
  447. } else {
  448. inode->i_mode |= S_IFREG;
  449. /* treat the dos attribute of read-only as read-only
  450. mode e.g. 555 */
  451. if (cifsInfo->cifsAttrs & ATTR_READONLY)
  452. inode->i_mode &= ~(S_IWUGO);
  453. /* BB add code here -
  454. validate if device or weird share or device type? */
  455. }
  456. if (is_size_safe_to_change(cifsInfo)) {
  457. /* can not safely change the file size here if the
  458. client is writing to it due to potential races */
  459. i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
  460. /* 512 bytes (2**9) is the fake blocksize that must be
  461. used for this calculation */
  462. inode->i_blocks = (512 - 1 + le64_to_cpu(
  463. pfindData->AllocationSize)) >> 9;
  464. }
  465. inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
  466. /* BB fill in uid and gid here? with help from winbind?
  467. or retrieve from NTFS stream extended attribute */
  468. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  469. /* fill in uid, gid, mode from server ACL */
  470. get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
  471. } else if (atomic_read(&cifsInfo->inUse) == 0) {
  472. inode->i_uid = cifs_sb->mnt_uid;
  473. inode->i_gid = cifs_sb->mnt_gid;
  474. /* set so we do not keep refreshing these fields with
  475. bad data after user has changed them in memory */
  476. atomic_set(&cifsInfo->inUse,1);
  477. }
  478. if (S_ISREG(inode->i_mode)) {
  479. cFYI(1, ("File inode"));
  480. inode->i_op = &cifs_file_inode_ops;
  481. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  482. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  483. inode->i_fop =
  484. &cifs_file_direct_nobrl_ops;
  485. else
  486. inode->i_fop = &cifs_file_direct_ops;
  487. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  488. inode->i_fop = &cifs_file_nobrl_ops;
  489. else /* not direct, send byte range locks */
  490. inode->i_fop = &cifs_file_ops;
  491. if(pTcon->ses->server->maxBuf <
  492. PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
  493. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  494. else
  495. inode->i_data.a_ops = &cifs_addr_ops;
  496. } else if (S_ISDIR(inode->i_mode)) {
  497. cFYI(1, ("Directory inode"));
  498. inode->i_op = &cifs_dir_inode_ops;
  499. inode->i_fop = &cifs_dir_ops;
  500. } else if (S_ISLNK(inode->i_mode)) {
  501. cFYI(1, ("Symbolic Link inode"));
  502. inode->i_op = &cifs_symlink_inode_ops;
  503. } else {
  504. init_special_inode(inode, inode->i_mode,
  505. inode->i_rdev);
  506. }
  507. }
  508. kfree(buf);
  509. return rc;
  510. }
  511. /* gets root inode */
  512. void cifs_read_inode(struct inode *inode)
  513. {
  514. int xid;
  515. struct cifs_sb_info *cifs_sb;
  516. cifs_sb = CIFS_SB(inode->i_sb);
  517. xid = GetXid();
  518. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
  519. cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
  520. else
  521. cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
  522. /* can not call macro FreeXid here since in a void func */
  523. _FreeXid(xid);
  524. }
  525. int cifs_unlink(struct inode *inode, struct dentry *direntry)
  526. {
  527. int rc = 0;
  528. int xid;
  529. struct cifs_sb_info *cifs_sb;
  530. struct cifsTconInfo *pTcon;
  531. char *full_path = NULL;
  532. struct cifsInodeInfo *cifsInode;
  533. FILE_BASIC_INFO *pinfo_buf;
  534. cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
  535. xid = GetXid();
  536. if(inode)
  537. cifs_sb = CIFS_SB(inode->i_sb);
  538. else
  539. cifs_sb = CIFS_SB(direntry->d_sb);
  540. pTcon = cifs_sb->tcon;
  541. /* Unlink can be called from rename so we can not grab the sem here
  542. since we deadlock otherwise */
  543. /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
  544. full_path = build_path_from_dentry(direntry);
  545. /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
  546. if (full_path == NULL) {
  547. FreeXid(xid);
  548. return -ENOMEM;
  549. }
  550. rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
  551. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  552. if (!rc) {
  553. if (direntry->d_inode)
  554. drop_nlink(direntry->d_inode);
  555. } else if (rc == -ENOENT) {
  556. d_drop(direntry);
  557. } else if (rc == -ETXTBSY) {
  558. int oplock = FALSE;
  559. __u16 netfid;
  560. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
  561. CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
  562. &netfid, &oplock, NULL, cifs_sb->local_nls,
  563. cifs_sb->mnt_cifs_flags &
  564. CIFS_MOUNT_MAP_SPECIAL_CHR);
  565. if (rc==0) {
  566. CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
  567. cifs_sb->local_nls,
  568. cifs_sb->mnt_cifs_flags &
  569. CIFS_MOUNT_MAP_SPECIAL_CHR);
  570. CIFSSMBClose(xid, pTcon, netfid);
  571. if (direntry->d_inode)
  572. drop_nlink(direntry->d_inode);
  573. }
  574. } else if (rc == -EACCES) {
  575. /* try only if r/o attribute set in local lookup data? */
  576. pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
  577. if (pinfo_buf) {
  578. /* ATTRS set to normal clears r/o bit */
  579. pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
  580. if (!(pTcon->ses->flags & CIFS_SES_NT4))
  581. rc = CIFSSMBSetTimes(xid, pTcon, full_path,
  582. pinfo_buf,
  583. cifs_sb->local_nls,
  584. cifs_sb->mnt_cifs_flags &
  585. CIFS_MOUNT_MAP_SPECIAL_CHR);
  586. else
  587. rc = -EOPNOTSUPP;
  588. if (rc == -EOPNOTSUPP) {
  589. int oplock = FALSE;
  590. __u16 netfid;
  591. /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
  592. full_path,
  593. (__u16)ATTR_NORMAL,
  594. cifs_sb->local_nls);
  595. For some strange reason it seems that NT4 eats the
  596. old setattr call without actually setting the
  597. attributes so on to the third attempted workaround
  598. */
  599. /* BB could scan to see if we already have it open
  600. and pass in pid of opener to function */
  601. rc = CIFSSMBOpen(xid, pTcon, full_path,
  602. FILE_OPEN, SYNCHRONIZE |
  603. FILE_WRITE_ATTRIBUTES, 0,
  604. &netfid, &oplock, NULL,
  605. cifs_sb->local_nls,
  606. cifs_sb->mnt_cifs_flags &
  607. CIFS_MOUNT_MAP_SPECIAL_CHR);
  608. if (rc==0) {
  609. rc = CIFSSMBSetFileTimes(xid, pTcon,
  610. pinfo_buf,
  611. netfid);
  612. CIFSSMBClose(xid, pTcon, netfid);
  613. }
  614. }
  615. kfree(pinfo_buf);
  616. }
  617. if (rc==0) {
  618. rc = CIFSSMBDelFile(xid, pTcon, full_path,
  619. cifs_sb->local_nls,
  620. cifs_sb->mnt_cifs_flags &
  621. CIFS_MOUNT_MAP_SPECIAL_CHR);
  622. if (!rc) {
  623. if (direntry->d_inode)
  624. drop_nlink(direntry->d_inode);
  625. } else if (rc == -ETXTBSY) {
  626. int oplock = FALSE;
  627. __u16 netfid;
  628. rc = CIFSSMBOpen(xid, pTcon, full_path,
  629. FILE_OPEN, DELETE,
  630. CREATE_NOT_DIR |
  631. CREATE_DELETE_ON_CLOSE,
  632. &netfid, &oplock, NULL,
  633. cifs_sb->local_nls,
  634. cifs_sb->mnt_cifs_flags &
  635. CIFS_MOUNT_MAP_SPECIAL_CHR);
  636. if (rc==0) {
  637. CIFSSMBRenameOpenFile(xid, pTcon,
  638. netfid, NULL,
  639. cifs_sb->local_nls,
  640. cifs_sb->mnt_cifs_flags &
  641. CIFS_MOUNT_MAP_SPECIAL_CHR);
  642. CIFSSMBClose(xid, pTcon, netfid);
  643. if (direntry->d_inode)
  644. drop_nlink(direntry->d_inode);
  645. }
  646. /* BB if rc = -ETXTBUSY goto the rename logic BB */
  647. }
  648. }
  649. }
  650. if (direntry->d_inode) {
  651. cifsInode = CIFS_I(direntry->d_inode);
  652. cifsInode->time = 0; /* will force revalidate to get info
  653. when needed */
  654. direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
  655. }
  656. if(inode) {
  657. inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
  658. cifsInode = CIFS_I(inode);
  659. cifsInode->time = 0; /* force revalidate of dir as well */
  660. }
  661. kfree(full_path);
  662. FreeXid(xid);
  663. return rc;
  664. }
  665. int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
  666. {
  667. int rc = 0;
  668. int xid;
  669. struct cifs_sb_info *cifs_sb;
  670. struct cifsTconInfo *pTcon;
  671. char *full_path = NULL;
  672. struct inode *newinode = NULL;
  673. cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
  674. xid = GetXid();
  675. cifs_sb = CIFS_SB(inode->i_sb);
  676. pTcon = cifs_sb->tcon;
  677. full_path = build_path_from_dentry(direntry);
  678. if (full_path == NULL) {
  679. FreeXid(xid);
  680. return -ENOMEM;
  681. }
  682. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  683. rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
  684. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  685. if (rc) {
  686. cFYI(1, ("cifs_mkdir returned 0x%x", rc));
  687. d_drop(direntry);
  688. } else {
  689. inc_nlink(inode);
  690. if (pTcon->ses->capabilities & CAP_UNIX)
  691. rc = cifs_get_inode_info_unix(&newinode, full_path,
  692. inode->i_sb,xid);
  693. else
  694. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  695. inode->i_sb,xid);
  696. if (pTcon->nocase)
  697. direntry->d_op = &cifs_ci_dentry_ops;
  698. else
  699. direntry->d_op = &cifs_dentry_ops;
  700. d_instantiate(direntry, newinode);
  701. if (direntry->d_inode)
  702. direntry->d_inode->i_nlink = 2;
  703. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
  704. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  705. CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  706. mode,
  707. (__u64)current->fsuid,
  708. (__u64)current->fsgid,
  709. 0 /* dev_t */,
  710. cifs_sb->local_nls,
  711. cifs_sb->mnt_cifs_flags &
  712. CIFS_MOUNT_MAP_SPECIAL_CHR);
  713. } else {
  714. CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  715. mode, (__u64)-1,
  716. (__u64)-1, 0 /* dev_t */,
  717. cifs_sb->local_nls,
  718. cifs_sb->mnt_cifs_flags &
  719. CIFS_MOUNT_MAP_SPECIAL_CHR);
  720. }
  721. else {
  722. /* BB to be implemented via Windows secrty descriptors
  723. eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
  724. -1, -1, local_nls); */
  725. if(direntry->d_inode) {
  726. direntry->d_inode->i_mode = mode;
  727. direntry->d_inode->i_mode |= S_IFDIR;
  728. if(cifs_sb->mnt_cifs_flags &
  729. CIFS_MOUNT_SET_UID) {
  730. direntry->d_inode->i_uid =
  731. current->fsuid;
  732. direntry->d_inode->i_gid =
  733. current->fsgid;
  734. }
  735. }
  736. }
  737. }
  738. kfree(full_path);
  739. FreeXid(xid);
  740. return rc;
  741. }
  742. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  743. {
  744. int rc = 0;
  745. int xid;
  746. struct cifs_sb_info *cifs_sb;
  747. struct cifsTconInfo *pTcon;
  748. char *full_path = NULL;
  749. struct cifsInodeInfo *cifsInode;
  750. cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
  751. xid = GetXid();
  752. cifs_sb = CIFS_SB(inode->i_sb);
  753. pTcon = cifs_sb->tcon;
  754. full_path = build_path_from_dentry(direntry);
  755. if (full_path == NULL) {
  756. FreeXid(xid);
  757. return -ENOMEM;
  758. }
  759. rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
  760. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  761. if (!rc) {
  762. drop_nlink(inode);
  763. i_size_write(direntry->d_inode,0);
  764. clear_nlink(direntry->d_inode);
  765. }
  766. cifsInode = CIFS_I(direntry->d_inode);
  767. cifsInode->time = 0; /* force revalidate to go get info when
  768. needed */
  769. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  770. current_fs_time(inode->i_sb);
  771. kfree(full_path);
  772. FreeXid(xid);
  773. return rc;
  774. }
  775. int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
  776. struct inode *target_inode, struct dentry *target_direntry)
  777. {
  778. char *fromName;
  779. char *toName;
  780. struct cifs_sb_info *cifs_sb_source;
  781. struct cifs_sb_info *cifs_sb_target;
  782. struct cifsTconInfo *pTcon;
  783. int xid;
  784. int rc = 0;
  785. xid = GetXid();
  786. cifs_sb_target = CIFS_SB(target_inode->i_sb);
  787. cifs_sb_source = CIFS_SB(source_inode->i_sb);
  788. pTcon = cifs_sb_source->tcon;
  789. if (pTcon != cifs_sb_target->tcon) {
  790. FreeXid(xid);
  791. return -EXDEV; /* BB actually could be allowed if same server,
  792. but different share.
  793. Might eventually add support for this */
  794. }
  795. /* we already have the rename sem so we do not need to grab it again
  796. here to protect the path integrity */
  797. fromName = build_path_from_dentry(source_direntry);
  798. toName = build_path_from_dentry(target_direntry);
  799. if ((fromName == NULL) || (toName == NULL)) {
  800. rc = -ENOMEM;
  801. goto cifs_rename_exit;
  802. }
  803. rc = CIFSSMBRename(xid, pTcon, fromName, toName,
  804. cifs_sb_source->local_nls,
  805. cifs_sb_source->mnt_cifs_flags &
  806. CIFS_MOUNT_MAP_SPECIAL_CHR);
  807. if (rc == -EEXIST) {
  808. /* check if they are the same file because rename of hardlinked
  809. files is a noop */
  810. FILE_UNIX_BASIC_INFO *info_buf_source;
  811. FILE_UNIX_BASIC_INFO *info_buf_target;
  812. info_buf_source =
  813. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  814. if (info_buf_source != NULL) {
  815. info_buf_target = info_buf_source + 1;
  816. if (pTcon->ses->capabilities & CAP_UNIX)
  817. rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
  818. info_buf_source,
  819. cifs_sb_source->local_nls,
  820. cifs_sb_source->mnt_cifs_flags &
  821. CIFS_MOUNT_MAP_SPECIAL_CHR);
  822. /* else rc is still EEXIST so will fall through to
  823. unlink the target and retry rename */
  824. if (rc == 0) {
  825. rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
  826. info_buf_target,
  827. cifs_sb_target->local_nls,
  828. /* remap based on source sb */
  829. cifs_sb_source->mnt_cifs_flags &
  830. CIFS_MOUNT_MAP_SPECIAL_CHR);
  831. }
  832. if ((rc == 0) &&
  833. (info_buf_source->UniqueId ==
  834. info_buf_target->UniqueId)) {
  835. /* do not rename since the files are hardlinked which
  836. is a noop */
  837. } else {
  838. /* we either can not tell the files are hardlinked
  839. (as with Windows servers) or files are not
  840. hardlinked so delete the target manually before
  841. renaming to follow POSIX rather than Windows
  842. semantics */
  843. cifs_unlink(target_inode, target_direntry);
  844. rc = CIFSSMBRename(xid, pTcon, fromName,
  845. toName,
  846. cifs_sb_source->local_nls,
  847. cifs_sb_source->mnt_cifs_flags
  848. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  849. }
  850. kfree(info_buf_source);
  851. } /* if we can not get memory just leave rc as EEXIST */
  852. }
  853. if (rc) {
  854. cFYI(1, ("rename rc %d", rc));
  855. }
  856. if ((rc == -EIO) || (rc == -EEXIST)) {
  857. int oplock = FALSE;
  858. __u16 netfid;
  859. /* BB FIXME Is Generic Read correct for rename? */
  860. /* if renaming directory - we should not say CREATE_NOT_DIR,
  861. need to test renaming open directory, also GENERIC_READ
  862. might not right be right access to request */
  863. rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
  864. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  865. cifs_sb_source->local_nls,
  866. cifs_sb_source->mnt_cifs_flags &
  867. CIFS_MOUNT_MAP_SPECIAL_CHR);
  868. if (rc==0) {
  869. rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
  870. cifs_sb_source->local_nls,
  871. cifs_sb_source->mnt_cifs_flags &
  872. CIFS_MOUNT_MAP_SPECIAL_CHR);
  873. CIFSSMBClose(xid, pTcon, netfid);
  874. }
  875. }
  876. cifs_rename_exit:
  877. kfree(fromName);
  878. kfree(toName);
  879. FreeXid(xid);
  880. return rc;
  881. }
  882. int cifs_revalidate(struct dentry *direntry)
  883. {
  884. int xid;
  885. int rc = 0;
  886. char *full_path;
  887. struct cifs_sb_info *cifs_sb;
  888. struct cifsInodeInfo *cifsInode;
  889. loff_t local_size;
  890. struct timespec local_mtime;
  891. int invalidate_inode = FALSE;
  892. if (direntry->d_inode == NULL)
  893. return -ENOENT;
  894. cifsInode = CIFS_I(direntry->d_inode);
  895. if (cifsInode == NULL)
  896. return -ENOENT;
  897. /* no sense revalidating inode info on file that no one can write */
  898. if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
  899. return rc;
  900. xid = GetXid();
  901. cifs_sb = CIFS_SB(direntry->d_sb);
  902. /* can not safely grab the rename sem here if rename calls revalidate
  903. since that would deadlock */
  904. full_path = build_path_from_dentry(direntry);
  905. if (full_path == NULL) {
  906. FreeXid(xid);
  907. return -ENOMEM;
  908. }
  909. cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
  910. "jiffies %ld", full_path, direntry->d_inode,
  911. direntry->d_inode->i_count.counter, direntry,
  912. direntry->d_time, jiffies));
  913. if (cifsInode->time == 0) {
  914. /* was set to zero previously to force revalidate */
  915. } else if (time_before(jiffies, cifsInode->time + HZ) &&
  916. lookupCacheEnabled) {
  917. if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
  918. (direntry->d_inode->i_nlink == 1)) {
  919. kfree(full_path);
  920. FreeXid(xid);
  921. return rc;
  922. } else {
  923. cFYI(1, ("Have to revalidate file due to hardlinks"));
  924. }
  925. }
  926. /* save mtime and size */
  927. local_mtime = direntry->d_inode->i_mtime;
  928. local_size = direntry->d_inode->i_size;
  929. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
  930. rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
  931. direntry->d_sb,xid);
  932. if (rc) {
  933. cFYI(1, ("error on getting revalidate info %d", rc));
  934. /* if (rc != -ENOENT)
  935. rc = 0; */ /* BB should we cache info on
  936. certain errors? */
  937. }
  938. } else {
  939. rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
  940. direntry->d_sb,xid);
  941. if (rc) {
  942. cFYI(1, ("error on getting revalidate info %d", rc));
  943. /* if (rc != -ENOENT)
  944. rc = 0; */ /* BB should we cache info on
  945. certain errors? */
  946. }
  947. }
  948. /* should we remap certain errors, access denied?, to zero */
  949. /* if not oplocked, we invalidate inode pages if mtime or file size
  950. had changed on server */
  951. if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
  952. (local_size == direntry->d_inode->i_size)) {
  953. cFYI(1, ("cifs_revalidate - inode unchanged"));
  954. } else {
  955. /* file may have changed on server */
  956. if (cifsInode->clientCanCacheRead) {
  957. /* no need to invalidate inode pages since we were the
  958. only ones who could have modified the file and the
  959. server copy is staler than ours */
  960. } else {
  961. invalidate_inode = TRUE;
  962. }
  963. }
  964. /* can not grab this sem since kernel filesys locking documentation
  965. indicates i_mutex may be taken by the kernel on lookup and rename
  966. which could deadlock if we grab the i_mutex here as well */
  967. /* mutex_lock(&direntry->d_inode->i_mutex);*/
  968. /* need to write out dirty pages here */
  969. if (direntry->d_inode->i_mapping) {
  970. /* do we need to lock inode until after invalidate completes
  971. below? */
  972. filemap_fdatawrite(direntry->d_inode->i_mapping);
  973. }
  974. if (invalidate_inode) {
  975. /* shrink_dcache not necessary now that cifs dentry ops
  976. are exported for negative dentries */
  977. /* if(S_ISDIR(direntry->d_inode->i_mode))
  978. shrink_dcache_parent(direntry); */
  979. if (S_ISREG(direntry->d_inode->i_mode)) {
  980. if (direntry->d_inode->i_mapping)
  981. filemap_fdatawait(direntry->d_inode->i_mapping);
  982. /* may eventually have to do this for open files too */
  983. if (list_empty(&(cifsInode->openFileList))) {
  984. /* changed on server - flush read ahead pages */
  985. cFYI(1, ("Invalidating read ahead data on "
  986. "closed file"));
  987. invalidate_remote_inode(direntry->d_inode);
  988. }
  989. }
  990. }
  991. /* mutex_unlock(&direntry->d_inode->i_mutex); */
  992. kfree(full_path);
  993. FreeXid(xid);
  994. return rc;
  995. }
  996. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  997. struct kstat *stat)
  998. {
  999. int err = cifs_revalidate(dentry);
  1000. if (!err) {
  1001. generic_fillattr(dentry->d_inode, stat);
  1002. stat->blksize = CIFS_MAX_MSGSIZE;
  1003. }
  1004. return err;
  1005. }
  1006. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1007. {
  1008. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1009. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1010. struct page *page;
  1011. char *kaddr;
  1012. int rc = 0;
  1013. page = grab_cache_page(mapping, index);
  1014. if (!page)
  1015. return -ENOMEM;
  1016. kaddr = kmap_atomic(page, KM_USER0);
  1017. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  1018. flush_dcache_page(page);
  1019. kunmap_atomic(kaddr, KM_USER0);
  1020. unlock_page(page);
  1021. page_cache_release(page);
  1022. return rc;
  1023. }
  1024. int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1025. {
  1026. int xid;
  1027. struct cifs_sb_info *cifs_sb;
  1028. struct cifsTconInfo *pTcon;
  1029. char *full_path = NULL;
  1030. int rc = -EACCES;
  1031. struct cifsFileInfo *open_file = NULL;
  1032. FILE_BASIC_INFO time_buf;
  1033. int set_time = FALSE;
  1034. __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
  1035. __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
  1036. __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
  1037. struct cifsInodeInfo *cifsInode;
  1038. xid = GetXid();
  1039. cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
  1040. direntry->d_name.name, attrs->ia_valid));
  1041. cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
  1042. pTcon = cifs_sb->tcon;
  1043. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1044. /* check if we have permission to change attrs */
  1045. rc = inode_change_ok(direntry->d_inode, attrs);
  1046. if(rc < 0) {
  1047. FreeXid(xid);
  1048. return rc;
  1049. } else
  1050. rc = 0;
  1051. }
  1052. full_path = build_path_from_dentry(direntry);
  1053. if (full_path == NULL) {
  1054. FreeXid(xid);
  1055. return -ENOMEM;
  1056. }
  1057. cifsInode = CIFS_I(direntry->d_inode);
  1058. /* BB check if we need to refresh inode from server now ? BB */
  1059. /* need to flush data before changing file size on server */
  1060. filemap_write_and_wait(direntry->d_inode->i_mapping);
  1061. if (attrs->ia_valid & ATTR_SIZE) {
  1062. /* To avoid spurious oplock breaks from server, in the case of
  1063. inodes that we already have open, avoid doing path based
  1064. setting of file size if we can do it by handle.
  1065. This keeps our caching token (oplock) and avoids timeouts
  1066. when the local oplock break takes longer to flush
  1067. writebehind data than the SMB timeout for the SetPathInfo
  1068. request would allow */
  1069. open_file = find_writable_file(cifsInode);
  1070. if (open_file) {
  1071. __u16 nfid = open_file->netfid;
  1072. __u32 npid = open_file->pid;
  1073. rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
  1074. nfid, npid, FALSE);
  1075. atomic_dec(&open_file->wrtPending);
  1076. cFYI(1,("SetFSize for attrs rc = %d", rc));
  1077. if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1078. int bytes_written;
  1079. rc = CIFSSMBWrite(xid, pTcon,
  1080. nfid, 0, attrs->ia_size,
  1081. &bytes_written, NULL, NULL,
  1082. 1 /* 45 seconds */);
  1083. cFYI(1,("Wrt seteof rc %d", rc));
  1084. }
  1085. } else
  1086. rc = -EINVAL;
  1087. if (rc != 0) {
  1088. /* Set file size by pathname rather than by handle
  1089. either because no valid, writeable file handle for
  1090. it was found or because there was an error setting
  1091. it by handle */
  1092. rc = CIFSSMBSetEOF(xid, pTcon, full_path,
  1093. attrs->ia_size, FALSE,
  1094. cifs_sb->local_nls,
  1095. cifs_sb->mnt_cifs_flags &
  1096. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1097. cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
  1098. if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1099. __u16 netfid;
  1100. int oplock = FALSE;
  1101. rc = SMBLegacyOpen(xid, pTcon, full_path,
  1102. FILE_OPEN,
  1103. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  1104. CREATE_NOT_DIR, &netfid, &oplock,
  1105. NULL, cifs_sb->local_nls,
  1106. cifs_sb->mnt_cifs_flags &
  1107. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1108. if (rc==0) {
  1109. int bytes_written;
  1110. rc = CIFSSMBWrite(xid, pTcon,
  1111. netfid, 0,
  1112. attrs->ia_size,
  1113. &bytes_written, NULL,
  1114. NULL, 1 /* 45 sec */);
  1115. cFYI(1,("wrt seteof rc %d",rc));
  1116. CIFSSMBClose(xid, pTcon, netfid);
  1117. }
  1118. }
  1119. }
  1120. /* Server is ok setting allocation size implicitly - no need
  1121. to call:
  1122. CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
  1123. cifs_sb->local_nls);
  1124. */
  1125. if (rc == 0) {
  1126. rc = vmtruncate(direntry->d_inode, attrs->ia_size);
  1127. cifs_truncate_page(direntry->d_inode->i_mapping,
  1128. direntry->d_inode->i_size);
  1129. } else
  1130. goto cifs_setattr_exit;
  1131. }
  1132. if (attrs->ia_valid & ATTR_UID) {
  1133. cFYI(1, ("UID changed to %d", attrs->ia_uid));
  1134. uid = attrs->ia_uid;
  1135. }
  1136. if (attrs->ia_valid & ATTR_GID) {
  1137. cFYI(1, ("GID changed to %d", attrs->ia_gid));
  1138. gid = attrs->ia_gid;
  1139. }
  1140. time_buf.Attributes = 0;
  1141. if (attrs->ia_valid & ATTR_MODE) {
  1142. cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
  1143. mode = attrs->ia_mode;
  1144. }
  1145. if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
  1146. && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
  1147. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
  1148. 0 /* dev_t */, cifs_sb->local_nls,
  1149. cifs_sb->mnt_cifs_flags &
  1150. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1151. else if (attrs->ia_valid & ATTR_MODE) {
  1152. rc = 0;
  1153. if ((mode & S_IWUGO) == 0) /* not writeable */ {
  1154. if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
  1155. time_buf.Attributes =
  1156. cpu_to_le32(cifsInode->cifsAttrs |
  1157. ATTR_READONLY);
  1158. } else if ((mode & S_IWUGO) == S_IWUGO) {
  1159. if (cifsInode->cifsAttrs & ATTR_READONLY)
  1160. time_buf.Attributes =
  1161. cpu_to_le32(cifsInode->cifsAttrs &
  1162. (~ATTR_READONLY));
  1163. }
  1164. /* BB to be implemented -
  1165. via Windows security descriptors or streams */
  1166. /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
  1167. cifs_sb->local_nls); */
  1168. }
  1169. if (attrs->ia_valid & ATTR_ATIME) {
  1170. set_time = TRUE;
  1171. time_buf.LastAccessTime =
  1172. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1173. } else
  1174. time_buf.LastAccessTime = 0;
  1175. if (attrs->ia_valid & ATTR_MTIME) {
  1176. set_time = TRUE;
  1177. time_buf.LastWriteTime =
  1178. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1179. } else
  1180. time_buf.LastWriteTime = 0;
  1181. /* Do not set ctime explicitly unless other time
  1182. stamps are changed explicitly (i.e. by utime()
  1183. since we would then have a mix of client and
  1184. server times */
  1185. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1186. set_time = TRUE;
  1187. /* Although Samba throws this field away
  1188. it may be useful to Windows - but we do
  1189. not want to set ctime unless some other
  1190. timestamp is changing */
  1191. cFYI(1, ("CIFS - CTIME changed"));
  1192. time_buf.ChangeTime =
  1193. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1194. } else
  1195. time_buf.ChangeTime = 0;
  1196. if (set_time || time_buf.Attributes) {
  1197. time_buf.CreationTime = 0; /* do not change */
  1198. /* In the future we should experiment - try setting timestamps
  1199. via Handle (SetFileInfo) instead of by path */
  1200. if (!(pTcon->ses->flags & CIFS_SES_NT4))
  1201. rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
  1202. cifs_sb->local_nls,
  1203. cifs_sb->mnt_cifs_flags &
  1204. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1205. else
  1206. rc = -EOPNOTSUPP;
  1207. if (rc == -EOPNOTSUPP) {
  1208. int oplock = FALSE;
  1209. __u16 netfid;
  1210. cFYI(1, ("calling SetFileInfo since SetPathInfo for "
  1211. "times not supported by this server"));
  1212. /* BB we could scan to see if we already have it open
  1213. and pass in pid of opener to function */
  1214. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
  1215. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  1216. CREATE_NOT_DIR, &netfid, &oplock,
  1217. NULL, cifs_sb->local_nls,
  1218. cifs_sb->mnt_cifs_flags &
  1219. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1220. if (rc==0) {
  1221. rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
  1222. netfid);
  1223. CIFSSMBClose(xid, pTcon, netfid);
  1224. } else {
  1225. /* BB For even older servers we could convert time_buf
  1226. into old DOS style which uses two second
  1227. granularity */
  1228. /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
  1229. &time_buf, cifs_sb->local_nls); */
  1230. }
  1231. }
  1232. /* Even if error on time set, no sense failing the call if
  1233. the server would set the time to a reasonable value anyway,
  1234. and this check ensures that we are not being called from
  1235. sys_utimes in which case we ought to fail the call back to
  1236. the user when the server rejects the call */
  1237. if((rc) && (attrs->ia_valid &&
  1238. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1239. rc = 0;
  1240. }
  1241. /* do not need local check to inode_check_ok since the server does
  1242. that */
  1243. if (!rc)
  1244. rc = inode_setattr(direntry->d_inode, attrs);
  1245. cifs_setattr_exit:
  1246. kfree(full_path);
  1247. FreeXid(xid);
  1248. return rc;
  1249. }
  1250. void cifs_delete_inode(struct inode *inode)
  1251. {
  1252. cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
  1253. /* may have to add back in if and when safe distributed caching of
  1254. directories added e.g. via FindNotify */
  1255. }