inode.c 58 KB

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