inode.c 64 KB

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