inode.c 52 KB

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