inode.c 41 KB

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