inode.c 62 KB

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