inode.c 52 KB

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