inode.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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) {
  460. cFYI(1, ("GetSrvInodeNum rc %d", rc1));
  461. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  462. /* disable serverino if call not supported */
  463. if (rc1 == -EINVAL)
  464. cifs_sb->mnt_cifs_flags &=
  465. ~CIFS_MOUNT_SERVER_INUM;
  466. }
  467. } else {
  468. fattr.cf_uniqueid = iunique(sb, ROOT_I);
  469. }
  470. } else {
  471. fattr.cf_uniqueid = CIFS_I(*pinode)->uniqueid;
  472. }
  473. /* query for SFU type info if supported and needed */
  474. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  475. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  476. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  477. if (tmprc)
  478. cFYI(1, ("cifs_sfu_type failed: %d", tmprc));
  479. }
  480. #ifdef CONFIG_CIFS_EXPERIMENTAL
  481. /* fill in 0777 bits from ACL */
  482. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  483. cFYI(1, ("Getting mode bits from ACL"));
  484. cifs_acl_to_fattr(cifs_sb, &fattr, *pinode, full_path, pfid);
  485. }
  486. #endif
  487. /* fill in remaining high mode bits e.g. SUID, VTX */
  488. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  489. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  490. if (!*pinode) {
  491. *pinode = cifs_iget(sb, &fattr);
  492. if (!*pinode)
  493. rc = -ENOMEM;
  494. } else {
  495. cifs_fattr_to_inode(*pinode, &fattr);
  496. }
  497. cgii_exit:
  498. kfree(buf);
  499. return rc;
  500. }
  501. static const struct inode_operations cifs_ipc_inode_ops = {
  502. .lookup = cifs_lookup,
  503. };
  504. char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb)
  505. {
  506. int pplen = cifs_sb->prepathlen;
  507. int dfsplen;
  508. char *full_path = NULL;
  509. /* if no prefix path, simply set path to the root of share to "" */
  510. if (pplen == 0) {
  511. full_path = kmalloc(1, GFP_KERNEL);
  512. if (full_path)
  513. full_path[0] = 0;
  514. return full_path;
  515. }
  516. if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
  517. dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
  518. else
  519. dfsplen = 0;
  520. full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
  521. if (full_path == NULL)
  522. return full_path;
  523. if (dfsplen) {
  524. strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
  525. /* switch slash direction in prepath depending on whether
  526. * windows or posix style path names
  527. */
  528. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  529. int i;
  530. for (i = 0; i < dfsplen; i++) {
  531. if (full_path[i] == '\\')
  532. full_path[i] = '/';
  533. }
  534. }
  535. }
  536. strncpy(full_path + dfsplen, cifs_sb->prepath, pplen);
  537. full_path[dfsplen + pplen] = 0; /* add trailing null */
  538. return full_path;
  539. }
  540. static int
  541. cifs_find_inode(struct inode *inode, void *opaque)
  542. {
  543. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  544. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  545. return 0;
  546. return 1;
  547. }
  548. static int
  549. cifs_init_inode(struct inode *inode, void *opaque)
  550. {
  551. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  552. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  553. return 0;
  554. }
  555. /* Given fattrs, get a corresponding inode */
  556. struct inode *
  557. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  558. {
  559. unsigned long hash;
  560. struct inode *inode;
  561. cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid));
  562. /* hash down to 32-bits on 32-bit arch */
  563. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  564. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  565. /* we have fattrs in hand, update the inode */
  566. if (inode) {
  567. cifs_fattr_to_inode(inode, fattr);
  568. if (sb->s_flags & MS_NOATIME)
  569. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  570. if (inode->i_state & I_NEW) {
  571. inode->i_ino = hash;
  572. unlock_new_inode(inode);
  573. }
  574. }
  575. return inode;
  576. }
  577. /* gets root inode */
  578. struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino)
  579. {
  580. int xid;
  581. struct cifs_sb_info *cifs_sb;
  582. struct inode *inode = NULL;
  583. long rc;
  584. char *full_path;
  585. cifs_sb = CIFS_SB(sb);
  586. full_path = cifs_build_path_to_root(cifs_sb);
  587. if (full_path == NULL)
  588. return ERR_PTR(-ENOMEM);
  589. xid = GetXid();
  590. if (cifs_sb->tcon->unix_ext)
  591. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  592. else
  593. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  594. xid, NULL);
  595. if (!inode)
  596. return ERR_PTR(-ENOMEM);
  597. if (rc && cifs_sb->tcon->ipc) {
  598. cFYI(1, ("ipc connection - fake read inode"));
  599. inode->i_mode |= S_IFDIR;
  600. inode->i_nlink = 2;
  601. inode->i_op = &cifs_ipc_inode_ops;
  602. inode->i_fop = &simple_dir_operations;
  603. inode->i_uid = cifs_sb->mnt_uid;
  604. inode->i_gid = cifs_sb->mnt_gid;
  605. } else if (rc) {
  606. kfree(full_path);
  607. _FreeXid(xid);
  608. iget_failed(inode);
  609. return ERR_PTR(rc);
  610. }
  611. kfree(full_path);
  612. /* can not call macro FreeXid here since in a void func
  613. * TODO: This is no longer true
  614. */
  615. _FreeXid(xid);
  616. return inode;
  617. }
  618. static int
  619. cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
  620. char *full_path, __u32 dosattr)
  621. {
  622. int rc;
  623. int oplock = 0;
  624. __u16 netfid;
  625. __u32 netpid;
  626. bool set_time = false;
  627. struct cifsFileInfo *open_file;
  628. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  629. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  630. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  631. FILE_BASIC_INFO info_buf;
  632. if (attrs == NULL)
  633. return -EINVAL;
  634. if (attrs->ia_valid & ATTR_ATIME) {
  635. set_time = true;
  636. info_buf.LastAccessTime =
  637. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  638. } else
  639. info_buf.LastAccessTime = 0;
  640. if (attrs->ia_valid & ATTR_MTIME) {
  641. set_time = true;
  642. info_buf.LastWriteTime =
  643. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  644. } else
  645. info_buf.LastWriteTime = 0;
  646. /*
  647. * Samba throws this field away, but windows may actually use it.
  648. * Do not set ctime unless other time stamps are changed explicitly
  649. * (i.e. by utimes()) since we would then have a mix of client and
  650. * server times.
  651. */
  652. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  653. cFYI(1, ("CIFS - CTIME changed"));
  654. info_buf.ChangeTime =
  655. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  656. } else
  657. info_buf.ChangeTime = 0;
  658. info_buf.CreationTime = 0; /* don't change */
  659. info_buf.Attributes = cpu_to_le32(dosattr);
  660. /*
  661. * If the file is already open for write, just use that fileid
  662. */
  663. open_file = find_writable_file(cifsInode);
  664. if (open_file) {
  665. netfid = open_file->netfid;
  666. netpid = open_file->pid;
  667. goto set_via_filehandle;
  668. }
  669. /*
  670. * NT4 apparently returns success on this call, but it doesn't
  671. * really work.
  672. */
  673. if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
  674. rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
  675. &info_buf, cifs_sb->local_nls,
  676. cifs_sb->mnt_cifs_flags &
  677. CIFS_MOUNT_MAP_SPECIAL_CHR);
  678. if (rc == 0) {
  679. cifsInode->cifsAttrs = dosattr;
  680. goto out;
  681. } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
  682. goto out;
  683. }
  684. cFYI(1, ("calling SetFileInfo since SetPathInfo for "
  685. "times not supported by this server"));
  686. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
  687. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  688. CREATE_NOT_DIR, &netfid, &oplock,
  689. NULL, cifs_sb->local_nls,
  690. cifs_sb->mnt_cifs_flags &
  691. CIFS_MOUNT_MAP_SPECIAL_CHR);
  692. if (rc != 0) {
  693. if (rc == -EIO)
  694. rc = -EINVAL;
  695. goto out;
  696. }
  697. netpid = current->tgid;
  698. set_via_filehandle:
  699. rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
  700. if (!rc)
  701. cifsInode->cifsAttrs = dosattr;
  702. if (open_file == NULL)
  703. CIFSSMBClose(xid, pTcon, netfid);
  704. else
  705. atomic_dec(&open_file->wrtPending);
  706. out:
  707. return rc;
  708. }
  709. /*
  710. * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  711. * and rename it to a random name that hopefully won't conflict with
  712. * anything else.
  713. */
  714. static int
  715. cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
  716. {
  717. int oplock = 0;
  718. int rc;
  719. __u16 netfid;
  720. struct inode *inode = dentry->d_inode;
  721. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  722. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  723. struct cifsTconInfo *tcon = cifs_sb->tcon;
  724. __u32 dosattr, origattr;
  725. FILE_BASIC_INFO *info_buf = NULL;
  726. rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
  727. DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
  728. &netfid, &oplock, NULL, cifs_sb->local_nls,
  729. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  730. if (rc != 0)
  731. goto out;
  732. origattr = cifsInode->cifsAttrs;
  733. if (origattr == 0)
  734. origattr |= ATTR_NORMAL;
  735. dosattr = origattr & ~ATTR_READONLY;
  736. if (dosattr == 0)
  737. dosattr |= ATTR_NORMAL;
  738. dosattr |= ATTR_HIDDEN;
  739. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  740. if (dosattr != origattr) {
  741. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  742. if (info_buf == NULL) {
  743. rc = -ENOMEM;
  744. goto out_close;
  745. }
  746. info_buf->Attributes = cpu_to_le32(dosattr);
  747. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  748. current->tgid);
  749. /* although we would like to mark the file hidden
  750. if that fails we will still try to rename it */
  751. if (rc != 0)
  752. cifsInode->cifsAttrs = dosattr;
  753. else
  754. dosattr = origattr; /* since not able to change them */
  755. }
  756. /* rename the file */
  757. rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
  758. cifs_sb->mnt_cifs_flags &
  759. CIFS_MOUNT_MAP_SPECIAL_CHR);
  760. if (rc != 0) {
  761. rc = -ETXTBSY;
  762. goto undo_setattr;
  763. }
  764. /* try to set DELETE_ON_CLOSE */
  765. if (!cifsInode->delete_pending) {
  766. rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
  767. current->tgid);
  768. /*
  769. * some samba versions return -ENOENT when we try to set the
  770. * file disposition here. Likely a samba bug, but work around
  771. * it for now. This means that some cifsXXX files may hang
  772. * around after they shouldn't.
  773. *
  774. * BB: remove this hack after more servers have the fix
  775. */
  776. if (rc == -ENOENT)
  777. rc = 0;
  778. else if (rc != 0) {
  779. rc = -ETXTBSY;
  780. goto undo_rename;
  781. }
  782. cifsInode->delete_pending = true;
  783. }
  784. out_close:
  785. CIFSSMBClose(xid, tcon, netfid);
  786. out:
  787. kfree(info_buf);
  788. return rc;
  789. /*
  790. * reset everything back to the original state. Don't bother
  791. * dealing with errors here since we can't do anything about
  792. * them anyway.
  793. */
  794. undo_rename:
  795. CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
  796. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  797. CIFS_MOUNT_MAP_SPECIAL_CHR);
  798. undo_setattr:
  799. if (dosattr != origattr) {
  800. info_buf->Attributes = cpu_to_le32(origattr);
  801. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
  802. current->tgid))
  803. cifsInode->cifsAttrs = origattr;
  804. }
  805. goto out_close;
  806. }
  807. /*
  808. * If dentry->d_inode is null (usually meaning the cached dentry
  809. * is a negative dentry) then we would attempt a standard SMB delete, but
  810. * if that fails we can not attempt the fall back mechanisms on EACESS
  811. * but will return the EACESS to the caller. Note that the VFS does not call
  812. * unlink on negative dentries currently.
  813. */
  814. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  815. {
  816. int rc = 0;
  817. int xid;
  818. char *full_path = NULL;
  819. struct inode *inode = dentry->d_inode;
  820. struct cifsInodeInfo *cifs_inode;
  821. struct super_block *sb = dir->i_sb;
  822. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  823. struct cifsTconInfo *tcon = cifs_sb->tcon;
  824. struct iattr *attrs = NULL;
  825. __u32 dosattr = 0, origattr = 0;
  826. cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
  827. xid = GetXid();
  828. /* Unlink can be called from rename so we can not take the
  829. * sb->s_vfs_rename_mutex here */
  830. full_path = build_path_from_dentry(dentry);
  831. if (full_path == NULL) {
  832. rc = -ENOMEM;
  833. FreeXid(xid);
  834. return rc;
  835. }
  836. if ((tcon->ses->capabilities & CAP_UNIX) &&
  837. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  838. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  839. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  840. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  841. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  842. cFYI(1, ("posix del rc %d", rc));
  843. if ((rc == 0) || (rc == -ENOENT))
  844. goto psx_del_no_retry;
  845. }
  846. retry_std_delete:
  847. rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
  848. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  849. psx_del_no_retry:
  850. if (!rc) {
  851. if (inode)
  852. drop_nlink(inode);
  853. } else if (rc == -ENOENT) {
  854. d_drop(dentry);
  855. } else if (rc == -ETXTBSY) {
  856. rc = cifs_rename_pending_delete(full_path, dentry, xid);
  857. if (rc == 0)
  858. drop_nlink(inode);
  859. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  860. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  861. if (attrs == NULL) {
  862. rc = -ENOMEM;
  863. goto out_reval;
  864. }
  865. /* try to reset dos attributes */
  866. cifs_inode = CIFS_I(inode);
  867. origattr = cifs_inode->cifsAttrs;
  868. if (origattr == 0)
  869. origattr |= ATTR_NORMAL;
  870. dosattr = origattr & ~ATTR_READONLY;
  871. if (dosattr == 0)
  872. dosattr |= ATTR_NORMAL;
  873. dosattr |= ATTR_HIDDEN;
  874. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  875. if (rc != 0)
  876. goto out_reval;
  877. goto retry_std_delete;
  878. }
  879. /* undo the setattr if we errored out and it's needed */
  880. if (rc != 0 && dosattr != 0)
  881. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  882. out_reval:
  883. if (inode) {
  884. cifs_inode = CIFS_I(inode);
  885. cifs_inode->time = 0; /* will force revalidate to get info
  886. when needed */
  887. inode->i_ctime = current_fs_time(sb);
  888. }
  889. dir->i_ctime = dir->i_mtime = current_fs_time(sb);
  890. cifs_inode = CIFS_I(dir);
  891. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  892. kfree(full_path);
  893. kfree(attrs);
  894. FreeXid(xid);
  895. return rc;
  896. }
  897. int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
  898. {
  899. int rc = 0, tmprc;
  900. int xid;
  901. struct cifs_sb_info *cifs_sb;
  902. struct cifsTconInfo *pTcon;
  903. char *full_path = NULL;
  904. struct inode *newinode = NULL;
  905. struct cifs_fattr fattr;
  906. cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
  907. xid = GetXid();
  908. cifs_sb = CIFS_SB(inode->i_sb);
  909. pTcon = cifs_sb->tcon;
  910. full_path = build_path_from_dentry(direntry);
  911. if (full_path == NULL) {
  912. rc = -ENOMEM;
  913. FreeXid(xid);
  914. return rc;
  915. }
  916. if ((pTcon->ses->capabilities & CAP_UNIX) &&
  917. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  918. le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
  919. u32 oplock = 0;
  920. FILE_UNIX_BASIC_INFO *pInfo =
  921. kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  922. if (pInfo == NULL) {
  923. rc = -ENOMEM;
  924. goto mkdir_out;
  925. }
  926. mode &= ~current_umask();
  927. rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
  928. mode, NULL /* netfid */, pInfo, &oplock,
  929. full_path, cifs_sb->local_nls,
  930. cifs_sb->mnt_cifs_flags &
  931. CIFS_MOUNT_MAP_SPECIAL_CHR);
  932. if (rc == -EOPNOTSUPP) {
  933. kfree(pInfo);
  934. goto mkdir_retry_old;
  935. } else if (rc) {
  936. cFYI(1, ("posix mkdir returned 0x%x", rc));
  937. d_drop(direntry);
  938. } else {
  939. if (pInfo->Type == cpu_to_le32(-1)) {
  940. /* no return info, go query for it */
  941. kfree(pInfo);
  942. goto mkdir_get_info;
  943. }
  944. /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
  945. to set uid/gid */
  946. inc_nlink(inode);
  947. if (pTcon->nocase)
  948. direntry->d_op = &cifs_ci_dentry_ops;
  949. else
  950. direntry->d_op = &cifs_dentry_ops;
  951. cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb);
  952. newinode = cifs_iget(inode->i_sb, &fattr);
  953. if (!newinode) {
  954. kfree(pInfo);
  955. goto mkdir_get_info;
  956. }
  957. d_instantiate(direntry, newinode);
  958. #ifdef CONFIG_CIFS_DEBUG2
  959. cFYI(1, ("instantiated dentry %p %s to inode %p",
  960. direntry, direntry->d_name.name, newinode));
  961. if (newinode->i_nlink != 2)
  962. cFYI(1, ("unexpected number of links %d",
  963. newinode->i_nlink));
  964. #endif
  965. }
  966. kfree(pInfo);
  967. goto mkdir_out;
  968. }
  969. mkdir_retry_old:
  970. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  971. rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
  972. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  973. if (rc) {
  974. cFYI(1, ("cifs_mkdir returned 0x%x", rc));
  975. d_drop(direntry);
  976. } else {
  977. mkdir_get_info:
  978. inc_nlink(inode);
  979. if (pTcon->unix_ext)
  980. rc = cifs_get_inode_info_unix(&newinode, full_path,
  981. inode->i_sb, xid);
  982. else
  983. rc = cifs_get_inode_info(&newinode, full_path, NULL,
  984. inode->i_sb, xid, NULL);
  985. if (pTcon->nocase)
  986. direntry->d_op = &cifs_ci_dentry_ops;
  987. else
  988. direntry->d_op = &cifs_dentry_ops;
  989. d_instantiate(direntry, newinode);
  990. /* setting nlink not necessary except in cases where we
  991. * failed to get it from the server or was set bogus */
  992. if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
  993. direntry->d_inode->i_nlink = 2;
  994. mode &= ~current_umask();
  995. /* must turn on setgid bit if parent dir has it */
  996. if (inode->i_mode & S_ISGID)
  997. mode |= S_ISGID;
  998. if (pTcon->unix_ext) {
  999. struct cifs_unix_set_info_args args = {
  1000. .mode = mode,
  1001. .ctime = NO_CHANGE_64,
  1002. .atime = NO_CHANGE_64,
  1003. .mtime = NO_CHANGE_64,
  1004. .device = 0,
  1005. };
  1006. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1007. args.uid = (__u64)current_fsuid();
  1008. if (inode->i_mode & S_ISGID)
  1009. args.gid = (__u64)inode->i_gid;
  1010. else
  1011. args.gid = (__u64)current_fsgid();
  1012. } else {
  1013. args.uid = NO_CHANGE_64;
  1014. args.gid = NO_CHANGE_64;
  1015. }
  1016. CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  1017. cifs_sb->local_nls,
  1018. cifs_sb->mnt_cifs_flags &
  1019. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1020. } else {
  1021. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1022. (mode & S_IWUGO) == 0) {
  1023. FILE_BASIC_INFO pInfo;
  1024. struct cifsInodeInfo *cifsInode;
  1025. u32 dosattrs;
  1026. memset(&pInfo, 0, sizeof(pInfo));
  1027. cifsInode = CIFS_I(newinode);
  1028. dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
  1029. pInfo.Attributes = cpu_to_le32(dosattrs);
  1030. tmprc = CIFSSMBSetPathInfo(xid, pTcon,
  1031. full_path, &pInfo,
  1032. cifs_sb->local_nls,
  1033. cifs_sb->mnt_cifs_flags &
  1034. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1035. if (tmprc == 0)
  1036. cifsInode->cifsAttrs = dosattrs;
  1037. }
  1038. if (direntry->d_inode) {
  1039. if (cifs_sb->mnt_cifs_flags &
  1040. CIFS_MOUNT_DYNPERM)
  1041. direntry->d_inode->i_mode =
  1042. (mode | S_IFDIR);
  1043. if (cifs_sb->mnt_cifs_flags &
  1044. CIFS_MOUNT_SET_UID) {
  1045. direntry->d_inode->i_uid =
  1046. current_fsuid();
  1047. if (inode->i_mode & S_ISGID)
  1048. direntry->d_inode->i_gid =
  1049. inode->i_gid;
  1050. else
  1051. direntry->d_inode->i_gid =
  1052. current_fsgid();
  1053. }
  1054. }
  1055. }
  1056. }
  1057. mkdir_out:
  1058. kfree(full_path);
  1059. FreeXid(xid);
  1060. return rc;
  1061. }
  1062. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1063. {
  1064. int rc = 0;
  1065. int xid;
  1066. struct cifs_sb_info *cifs_sb;
  1067. struct cifsTconInfo *pTcon;
  1068. char *full_path = NULL;
  1069. struct cifsInodeInfo *cifsInode;
  1070. cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
  1071. xid = GetXid();
  1072. cifs_sb = CIFS_SB(inode->i_sb);
  1073. pTcon = cifs_sb->tcon;
  1074. full_path = build_path_from_dentry(direntry);
  1075. if (full_path == NULL) {
  1076. rc = -ENOMEM;
  1077. FreeXid(xid);
  1078. return rc;
  1079. }
  1080. rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
  1081. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  1082. if (!rc) {
  1083. drop_nlink(inode);
  1084. spin_lock(&direntry->d_inode->i_lock);
  1085. i_size_write(direntry->d_inode, 0);
  1086. clear_nlink(direntry->d_inode);
  1087. spin_unlock(&direntry->d_inode->i_lock);
  1088. }
  1089. cifsInode = CIFS_I(direntry->d_inode);
  1090. cifsInode->time = 0; /* force revalidate to go get info when
  1091. needed */
  1092. cifsInode = CIFS_I(inode);
  1093. cifsInode->time = 0; /* force revalidate to get parent dir info
  1094. since cached search results now invalid */
  1095. direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
  1096. current_fs_time(inode->i_sb);
  1097. kfree(full_path);
  1098. FreeXid(xid);
  1099. return rc;
  1100. }
  1101. static int
  1102. cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
  1103. struct dentry *to_dentry, const char *toPath)
  1104. {
  1105. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1106. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1107. __u16 srcfid;
  1108. int oplock, rc;
  1109. /* try path-based rename first */
  1110. rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
  1111. cifs_sb->mnt_cifs_flags &
  1112. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1113. /*
  1114. * don't bother with rename by filehandle unless file is busy and
  1115. * source Note that cross directory moves do not work with
  1116. * rename by filehandle to various Windows servers.
  1117. */
  1118. if (rc == 0 || rc != -ETXTBSY)
  1119. return rc;
  1120. /* open the file to be renamed -- we need DELETE perms */
  1121. rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
  1122. CREATE_NOT_DIR, &srcfid, &oplock, NULL,
  1123. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1124. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1125. if (rc == 0) {
  1126. rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
  1127. (const char *) to_dentry->d_name.name,
  1128. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  1129. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1130. CIFSSMBClose(xid, pTcon, srcfid);
  1131. }
  1132. return rc;
  1133. }
  1134. int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
  1135. struct inode *target_dir, struct dentry *target_dentry)
  1136. {
  1137. char *fromName = NULL;
  1138. char *toName = NULL;
  1139. struct cifs_sb_info *cifs_sb_source;
  1140. struct cifs_sb_info *cifs_sb_target;
  1141. struct cifsTconInfo *tcon;
  1142. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1143. FILE_UNIX_BASIC_INFO *info_buf_target;
  1144. int xid, rc, tmprc;
  1145. cifs_sb_target = CIFS_SB(target_dir->i_sb);
  1146. cifs_sb_source = CIFS_SB(source_dir->i_sb);
  1147. tcon = cifs_sb_source->tcon;
  1148. xid = GetXid();
  1149. /*
  1150. * BB: this might be allowed if same server, but different share.
  1151. * Consider adding support for this
  1152. */
  1153. if (tcon != cifs_sb_target->tcon) {
  1154. rc = -EXDEV;
  1155. goto cifs_rename_exit;
  1156. }
  1157. /*
  1158. * we already have the rename sem so we do not need to
  1159. * grab it again here to protect the path integrity
  1160. */
  1161. fromName = build_path_from_dentry(source_dentry);
  1162. if (fromName == NULL) {
  1163. rc = -ENOMEM;
  1164. goto cifs_rename_exit;
  1165. }
  1166. toName = build_path_from_dentry(target_dentry);
  1167. if (toName == NULL) {
  1168. rc = -ENOMEM;
  1169. goto cifs_rename_exit;
  1170. }
  1171. rc = cifs_do_rename(xid, source_dentry, fromName,
  1172. target_dentry, toName);
  1173. if (rc == -EEXIST && tcon->unix_ext) {
  1174. /*
  1175. * Are src and dst hardlinks of same inode? We can
  1176. * only tell with unix extensions enabled
  1177. */
  1178. info_buf_source =
  1179. kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
  1180. GFP_KERNEL);
  1181. if (info_buf_source == NULL) {
  1182. rc = -ENOMEM;
  1183. goto cifs_rename_exit;
  1184. }
  1185. info_buf_target = info_buf_source + 1;
  1186. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
  1187. info_buf_source,
  1188. cifs_sb_source->local_nls,
  1189. cifs_sb_source->mnt_cifs_flags &
  1190. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1191. if (tmprc != 0)
  1192. goto unlink_target;
  1193. tmprc = CIFSSMBUnixQPathInfo(xid, tcon,
  1194. toName, info_buf_target,
  1195. cifs_sb_target->local_nls,
  1196. /* remap based on source sb */
  1197. cifs_sb_source->mnt_cifs_flags &
  1198. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1199. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1200. info_buf_target->UniqueId)) {
  1201. /* same file, POSIX says that this is a noop */
  1202. rc = 0;
  1203. goto cifs_rename_exit;
  1204. }
  1205. } /* else ... BB we could add the same check for Windows by
  1206. checking the UniqueId via FILE_INTERNAL_INFO */
  1207. unlink_target:
  1208. /* Try unlinking the target dentry if it's not negative */
  1209. if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
  1210. tmprc = cifs_unlink(target_dir, target_dentry);
  1211. if (tmprc)
  1212. goto cifs_rename_exit;
  1213. rc = cifs_do_rename(xid, source_dentry, fromName,
  1214. target_dentry, toName);
  1215. }
  1216. cifs_rename_exit:
  1217. kfree(info_buf_source);
  1218. kfree(fromName);
  1219. kfree(toName);
  1220. FreeXid(xid);
  1221. return rc;
  1222. }
  1223. int cifs_revalidate(struct dentry *direntry)
  1224. {
  1225. int xid;
  1226. int rc = 0, wbrc = 0;
  1227. char *full_path;
  1228. struct cifs_sb_info *cifs_sb;
  1229. struct cifsInodeInfo *cifsInode;
  1230. loff_t local_size;
  1231. struct timespec local_mtime;
  1232. bool invalidate_inode = false;
  1233. if (direntry->d_inode == NULL)
  1234. return -ENOENT;
  1235. cifsInode = CIFS_I(direntry->d_inode);
  1236. if (cifsInode == NULL)
  1237. return -ENOENT;
  1238. /* no sense revalidating inode info on file that no one can write */
  1239. if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
  1240. return rc;
  1241. xid = GetXid();
  1242. cifs_sb = CIFS_SB(direntry->d_sb);
  1243. /* can not safely grab the rename sem here if rename calls revalidate
  1244. since that would deadlock */
  1245. full_path = build_path_from_dentry(direntry);
  1246. if (full_path == NULL) {
  1247. rc = -ENOMEM;
  1248. FreeXid(xid);
  1249. return rc;
  1250. }
  1251. cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
  1252. "jiffies %ld", full_path, direntry->d_inode,
  1253. direntry->d_inode->i_count.counter, direntry,
  1254. direntry->d_time, jiffies));
  1255. if (cifsInode->time == 0) {
  1256. /* was set to zero previously to force revalidate */
  1257. } else if (time_before(jiffies, cifsInode->time + HZ) &&
  1258. lookupCacheEnabled) {
  1259. if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
  1260. (direntry->d_inode->i_nlink == 1)) {
  1261. kfree(full_path);
  1262. FreeXid(xid);
  1263. return rc;
  1264. } else {
  1265. cFYI(1, ("Have to revalidate file due to hardlinks"));
  1266. }
  1267. }
  1268. /* save mtime and size */
  1269. local_mtime = direntry->d_inode->i_mtime;
  1270. local_size = direntry->d_inode->i_size;
  1271. if (cifs_sb->tcon->unix_ext) {
  1272. rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
  1273. direntry->d_sb, xid);
  1274. if (rc) {
  1275. cFYI(1, ("error on getting revalidate info %d", rc));
  1276. /* if (rc != -ENOENT)
  1277. rc = 0; */ /* BB should we cache info on
  1278. certain errors? */
  1279. }
  1280. } else {
  1281. rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
  1282. direntry->d_sb, xid, NULL);
  1283. if (rc) {
  1284. cFYI(1, ("error on getting revalidate info %d", rc));
  1285. /* if (rc != -ENOENT)
  1286. rc = 0; */ /* BB should we cache info on
  1287. certain errors? */
  1288. }
  1289. }
  1290. /* should we remap certain errors, access denied?, to zero */
  1291. /* if not oplocked, we invalidate inode pages if mtime or file size
  1292. had changed on server */
  1293. if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
  1294. (local_size == direntry->d_inode->i_size)) {
  1295. cFYI(1, ("cifs_revalidate - inode unchanged"));
  1296. } else {
  1297. /* file may have changed on server */
  1298. if (cifsInode->clientCanCacheRead) {
  1299. /* no need to invalidate inode pages since we were the
  1300. only ones who could have modified the file and the
  1301. server copy is staler than ours */
  1302. } else {
  1303. invalidate_inode = true;
  1304. }
  1305. }
  1306. /* can not grab this sem since kernel filesys locking documentation
  1307. indicates i_mutex may be taken by the kernel on lookup and rename
  1308. which could deadlock if we grab the i_mutex here as well */
  1309. /* mutex_lock(&direntry->d_inode->i_mutex);*/
  1310. /* need to write out dirty pages here */
  1311. if (direntry->d_inode->i_mapping) {
  1312. /* do we need to lock inode until after invalidate completes
  1313. below? */
  1314. wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
  1315. if (wbrc)
  1316. CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
  1317. }
  1318. if (invalidate_inode) {
  1319. /* shrink_dcache not necessary now that cifs dentry ops
  1320. are exported for negative dentries */
  1321. /* if (S_ISDIR(direntry->d_inode->i_mode))
  1322. shrink_dcache_parent(direntry); */
  1323. if (S_ISREG(direntry->d_inode->i_mode)) {
  1324. if (direntry->d_inode->i_mapping) {
  1325. wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
  1326. if (wbrc)
  1327. CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
  1328. }
  1329. /* may eventually have to do this for open files too */
  1330. if (list_empty(&(cifsInode->openFileList))) {
  1331. /* changed on server - flush read ahead pages */
  1332. cFYI(1, ("Invalidating read ahead data on "
  1333. "closed file"));
  1334. invalidate_remote_inode(direntry->d_inode);
  1335. }
  1336. }
  1337. }
  1338. /* mutex_unlock(&direntry->d_inode->i_mutex); */
  1339. kfree(full_path);
  1340. FreeXid(xid);
  1341. return rc;
  1342. }
  1343. int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1344. struct kstat *stat)
  1345. {
  1346. int err = cifs_revalidate(dentry);
  1347. if (!err) {
  1348. generic_fillattr(dentry->d_inode, stat);
  1349. stat->blksize = CIFS_MAX_MSGSIZE;
  1350. stat->ino = CIFS_I(dentry->d_inode)->uniqueid;
  1351. }
  1352. return err;
  1353. }
  1354. static int cifs_truncate_page(struct address_space *mapping, loff_t from)
  1355. {
  1356. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1357. unsigned offset = from & (PAGE_CACHE_SIZE - 1);
  1358. struct page *page;
  1359. int rc = 0;
  1360. page = grab_cache_page(mapping, index);
  1361. if (!page)
  1362. return -ENOMEM;
  1363. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  1364. unlock_page(page);
  1365. page_cache_release(page);
  1366. return rc;
  1367. }
  1368. static int cifs_vmtruncate(struct inode *inode, loff_t offset)
  1369. {
  1370. struct address_space *mapping = inode->i_mapping;
  1371. unsigned long limit;
  1372. spin_lock(&inode->i_lock);
  1373. if (inode->i_size < offset)
  1374. goto do_expand;
  1375. /*
  1376. * truncation of in-use swapfiles is disallowed - it would cause
  1377. * subsequent swapout to scribble on the now-freed blocks.
  1378. */
  1379. if (IS_SWAPFILE(inode)) {
  1380. spin_unlock(&inode->i_lock);
  1381. goto out_busy;
  1382. }
  1383. i_size_write(inode, offset);
  1384. spin_unlock(&inode->i_lock);
  1385. /*
  1386. * unmap_mapping_range is called twice, first simply for efficiency
  1387. * so that truncate_inode_pages does fewer single-page unmaps. However
  1388. * after this first call, and before truncate_inode_pages finishes,
  1389. * it is possible for private pages to be COWed, which remain after
  1390. * truncate_inode_pages finishes, hence the second unmap_mapping_range
  1391. * call must be made for correctness.
  1392. */
  1393. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1394. truncate_inode_pages(mapping, offset);
  1395. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1396. goto out_truncate;
  1397. do_expand:
  1398. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1399. if (limit != RLIM_INFINITY && offset > limit) {
  1400. spin_unlock(&inode->i_lock);
  1401. goto out_sig;
  1402. }
  1403. if (offset > inode->i_sb->s_maxbytes) {
  1404. spin_unlock(&inode->i_lock);
  1405. goto out_big;
  1406. }
  1407. i_size_write(inode, offset);
  1408. spin_unlock(&inode->i_lock);
  1409. out_truncate:
  1410. if (inode->i_op->truncate)
  1411. inode->i_op->truncate(inode);
  1412. return 0;
  1413. out_sig:
  1414. send_sig(SIGXFSZ, current, 0);
  1415. out_big:
  1416. return -EFBIG;
  1417. out_busy:
  1418. return -ETXTBSY;
  1419. }
  1420. static int
  1421. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  1422. int xid, char *full_path)
  1423. {
  1424. int rc;
  1425. struct cifsFileInfo *open_file;
  1426. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1427. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1428. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1429. /*
  1430. * To avoid spurious oplock breaks from server, in the case of
  1431. * inodes that we already have open, avoid doing path based
  1432. * setting of file size if we can do it by handle.
  1433. * This keeps our caching token (oplock) and avoids timeouts
  1434. * when the local oplock break takes longer to flush
  1435. * writebehind data than the SMB timeout for the SetPathInfo
  1436. * request would allow
  1437. */
  1438. open_file = find_writable_file(cifsInode);
  1439. if (open_file) {
  1440. __u16 nfid = open_file->netfid;
  1441. __u32 npid = open_file->pid;
  1442. rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
  1443. npid, false);
  1444. atomic_dec(&open_file->wrtPending);
  1445. cFYI(1, ("SetFSize for attrs rc = %d", rc));
  1446. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1447. unsigned int bytes_written;
  1448. rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
  1449. &bytes_written, NULL, NULL, 1);
  1450. cFYI(1, ("Wrt seteof rc %d", rc));
  1451. }
  1452. } else
  1453. rc = -EINVAL;
  1454. if (rc != 0) {
  1455. /* Set file size by pathname rather than by handle
  1456. either because no valid, writeable file handle for
  1457. it was found or because there was an error setting
  1458. it by handle */
  1459. rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
  1460. false, cifs_sb->local_nls,
  1461. cifs_sb->mnt_cifs_flags &
  1462. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1463. cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
  1464. if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  1465. __u16 netfid;
  1466. int oplock = 0;
  1467. rc = SMBLegacyOpen(xid, pTcon, full_path,
  1468. FILE_OPEN, GENERIC_WRITE,
  1469. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  1470. cifs_sb->local_nls,
  1471. cifs_sb->mnt_cifs_flags &
  1472. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1473. if (rc == 0) {
  1474. unsigned int bytes_written;
  1475. rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
  1476. attrs->ia_size,
  1477. &bytes_written, NULL,
  1478. NULL, 1);
  1479. cFYI(1, ("wrt seteof rc %d", rc));
  1480. CIFSSMBClose(xid, pTcon, netfid);
  1481. }
  1482. }
  1483. }
  1484. if (rc == 0) {
  1485. cifsInode->server_eof = attrs->ia_size;
  1486. rc = cifs_vmtruncate(inode, attrs->ia_size);
  1487. cifs_truncate_page(inode->i_mapping, inode->i_size);
  1488. }
  1489. return rc;
  1490. }
  1491. static int
  1492. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  1493. {
  1494. int rc;
  1495. int xid;
  1496. char *full_path = NULL;
  1497. struct inode *inode = direntry->d_inode;
  1498. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1499. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1500. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1501. struct cifs_unix_set_info_args *args = NULL;
  1502. struct cifsFileInfo *open_file;
  1503. cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
  1504. direntry->d_name.name, attrs->ia_valid));
  1505. xid = GetXid();
  1506. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1507. /* check if we have permission to change attrs */
  1508. rc = inode_change_ok(inode, attrs);
  1509. if (rc < 0)
  1510. goto out;
  1511. else
  1512. rc = 0;
  1513. }
  1514. full_path = build_path_from_dentry(direntry);
  1515. if (full_path == NULL) {
  1516. rc = -ENOMEM;
  1517. goto out;
  1518. }
  1519. /*
  1520. * Attempt to flush data before changing attributes. We need to do
  1521. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1522. * ownership or mode then we may also need to do this. Here, we take
  1523. * the safe way out and just do the flush on all setattr requests. If
  1524. * the flush returns error, store it to report later and continue.
  1525. *
  1526. * BB: This should be smarter. Why bother flushing pages that
  1527. * will be truncated anyway? Also, should we error out here if
  1528. * the flush returns error?
  1529. */
  1530. rc = filemap_write_and_wait(inode->i_mapping);
  1531. if (rc != 0) {
  1532. cifsInode->write_behind_rc = rc;
  1533. rc = 0;
  1534. }
  1535. if (attrs->ia_valid & ATTR_SIZE) {
  1536. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1537. if (rc != 0)
  1538. goto out;
  1539. }
  1540. /* skip mode change if it's just for clearing setuid/setgid */
  1541. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1542. attrs->ia_valid &= ~ATTR_MODE;
  1543. args = kmalloc(sizeof(*args), GFP_KERNEL);
  1544. if (args == NULL) {
  1545. rc = -ENOMEM;
  1546. goto out;
  1547. }
  1548. /* set up the struct */
  1549. if (attrs->ia_valid & ATTR_MODE)
  1550. args->mode = attrs->ia_mode;
  1551. else
  1552. args->mode = NO_CHANGE_64;
  1553. if (attrs->ia_valid & ATTR_UID)
  1554. args->uid = attrs->ia_uid;
  1555. else
  1556. args->uid = NO_CHANGE_64;
  1557. if (attrs->ia_valid & ATTR_GID)
  1558. args->gid = attrs->ia_gid;
  1559. else
  1560. args->gid = NO_CHANGE_64;
  1561. if (attrs->ia_valid & ATTR_ATIME)
  1562. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  1563. else
  1564. args->atime = NO_CHANGE_64;
  1565. if (attrs->ia_valid & ATTR_MTIME)
  1566. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  1567. else
  1568. args->mtime = NO_CHANGE_64;
  1569. if (attrs->ia_valid & ATTR_CTIME)
  1570. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  1571. else
  1572. args->ctime = NO_CHANGE_64;
  1573. args->device = 0;
  1574. open_file = find_writable_file(cifsInode);
  1575. if (open_file) {
  1576. u16 nfid = open_file->netfid;
  1577. u32 npid = open_file->pid;
  1578. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  1579. atomic_dec(&open_file->wrtPending);
  1580. } else {
  1581. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  1582. cifs_sb->local_nls,
  1583. cifs_sb->mnt_cifs_flags &
  1584. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1585. }
  1586. if (!rc)
  1587. rc = inode_setattr(inode, attrs);
  1588. out:
  1589. kfree(args);
  1590. kfree(full_path);
  1591. FreeXid(xid);
  1592. return rc;
  1593. }
  1594. static int
  1595. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  1596. {
  1597. int xid;
  1598. struct inode *inode = direntry->d_inode;
  1599. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1600. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1601. char *full_path = NULL;
  1602. int rc = -EACCES;
  1603. __u32 dosattr = 0;
  1604. __u64 mode = NO_CHANGE_64;
  1605. xid = GetXid();
  1606. cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
  1607. direntry->d_name.name, attrs->ia_valid));
  1608. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
  1609. /* check if we have permission to change attrs */
  1610. rc = inode_change_ok(inode, attrs);
  1611. if (rc < 0) {
  1612. FreeXid(xid);
  1613. return rc;
  1614. } else
  1615. rc = 0;
  1616. }
  1617. full_path = build_path_from_dentry(direntry);
  1618. if (full_path == NULL) {
  1619. rc = -ENOMEM;
  1620. FreeXid(xid);
  1621. return rc;
  1622. }
  1623. /*
  1624. * Attempt to flush data before changing attributes. We need to do
  1625. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  1626. * ownership or mode then we may also need to do this. Here, we take
  1627. * the safe way out and just do the flush on all setattr requests. If
  1628. * the flush returns error, store it to report later and continue.
  1629. *
  1630. * BB: This should be smarter. Why bother flushing pages that
  1631. * will be truncated anyway? Also, should we error out here if
  1632. * the flush returns error?
  1633. */
  1634. rc = filemap_write_and_wait(inode->i_mapping);
  1635. if (rc != 0) {
  1636. cifsInode->write_behind_rc = rc;
  1637. rc = 0;
  1638. }
  1639. if (attrs->ia_valid & ATTR_SIZE) {
  1640. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  1641. if (rc != 0)
  1642. goto cifs_setattr_exit;
  1643. }
  1644. /*
  1645. * Without unix extensions we can't send ownership changes to the
  1646. * server, so silently ignore them. This is consistent with how
  1647. * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
  1648. * CIFSACL support + proper Windows to Unix idmapping, we may be
  1649. * able to support this in the future.
  1650. */
  1651. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  1652. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  1653. /* skip mode change if it's just for clearing setuid/setgid */
  1654. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  1655. attrs->ia_valid &= ~ATTR_MODE;
  1656. if (attrs->ia_valid & ATTR_MODE) {
  1657. cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
  1658. mode = attrs->ia_mode;
  1659. }
  1660. if (attrs->ia_valid & ATTR_MODE) {
  1661. rc = 0;
  1662. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1663. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
  1664. rc = mode_to_acl(inode, full_path, mode);
  1665. else
  1666. #endif
  1667. if (((mode & S_IWUGO) == 0) &&
  1668. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  1669. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  1670. /* fix up mode if we're not using dynperm */
  1671. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  1672. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  1673. } else if ((mode & S_IWUGO) &&
  1674. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  1675. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  1676. /* Attributes of 0 are ignored */
  1677. if (dosattr == 0)
  1678. dosattr |= ATTR_NORMAL;
  1679. /* reset local inode permissions to normal */
  1680. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1681. attrs->ia_mode &= ~(S_IALLUGO);
  1682. if (S_ISDIR(inode->i_mode))
  1683. attrs->ia_mode |=
  1684. cifs_sb->mnt_dir_mode;
  1685. else
  1686. attrs->ia_mode |=
  1687. cifs_sb->mnt_file_mode;
  1688. }
  1689. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  1690. /* ignore mode change - ATTR_READONLY hasn't changed */
  1691. attrs->ia_valid &= ~ATTR_MODE;
  1692. }
  1693. }
  1694. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  1695. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  1696. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1697. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  1698. /* Even if error on time set, no sense failing the call if
  1699. the server would set the time to a reasonable value anyway,
  1700. and this check ensures that we are not being called from
  1701. sys_utimes in which case we ought to fail the call back to
  1702. the user when the server rejects the call */
  1703. if ((rc) && (attrs->ia_valid &
  1704. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  1705. rc = 0;
  1706. }
  1707. /* do not need local check to inode_check_ok since the server does
  1708. that */
  1709. if (!rc)
  1710. rc = inode_setattr(inode, attrs);
  1711. cifs_setattr_exit:
  1712. kfree(full_path);
  1713. FreeXid(xid);
  1714. return rc;
  1715. }
  1716. int
  1717. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  1718. {
  1719. struct inode *inode = direntry->d_inode;
  1720. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1721. struct cifsTconInfo *pTcon = cifs_sb->tcon;
  1722. if (pTcon->unix_ext)
  1723. return cifs_setattr_unix(direntry, attrs);
  1724. return cifs_setattr_nounix(direntry, attrs);
  1725. /* BB: add cifs_setattr_legacy for really old servers */
  1726. }
  1727. #if 0
  1728. void cifs_delete_inode(struct inode *inode)
  1729. {
  1730. cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
  1731. /* may have to add back in if and when safe distributed caching of
  1732. directories added e.g. via FindNotify */
  1733. }
  1734. #endif