inode.c 52 KB

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